1

HttpWebRequest を介して Sitecore Credentials を渡すことは可能ですか? 以下のコードは、呼び出される asmx が匿名ユーザーとして実行されるという事実を除いて、うまく機能します。呼び出しているページにサイトコアの現在のユーザー資格情報を渡すことができるようにしたいと考えています。

CookieContainer cookieJar = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("ttp://localhost/file.asmx");
req.Headers.Add("SOAPAction", "\"h_ttp://tempuri.org/Register\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
req.ContentLength = 0;
req.CookieContainer = cookieJar;
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader respStrm = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
string responseITem = respStrm.ReadToEnd();
HttpContext.Current.Response.Write(responseITem);
HttpContext.Current.Response.End();
4

2 に答える 2

3

Sitecore ユーザーの認証情報は Cookie に保存されます。したがって、クライアント Cookie を http リクエストに追加できます。

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.CookieContainer = new CookieContainer();
HttpCookieCollection userCookies = Request.Cookies;
for (int userCookieCount = 0; userCookieCount < userCookies.Count; userCookieCount++)
{
    HttpCookie httpCookie = userCookies.Get(userCookieCount);
    Cookie cookie = new Cookie();
    /*  We have to add the target host because the cookie does not contain the domain information.
        In this case, this behaviour is not a security issue, because the target is our own platform.
        Further informations: http://stackoverflow.com/a/460990 
    */
    cookie.Domain = request.RequestUri.Host;
    cookie.Expires = httpCookie.Expires;
    cookie.Name = httpCookie.Name;
    cookie.Path = httpCookie.Path;
    cookie.Secure = httpCookie.Secure;
    cookie.Value = httpCookie.Value;

    request.CookieContainer.Add(cookie);
}

また、Sitecore エラー マネージャー モジュールを確認することもできます。そこで、クライアント Cookie を送信する http リクエストも作成します (149 ~ 170 行を参照)。

https://github.com/unic/SitecoreErrorManager/blob/master/Modules.ErrorManager/Controls/BaseError.cs

于 2013-03-06T07:37:38.223 に答える
2

現在のユーザー資格情報を要求に追加して、asmx Web サービスでそれらを取得し、その資格情報を使用してユーザーをログに記録し、コンテキストが設定されるようにする必要があります。

// add the credentials to the Post method
var credentials = "yourCredentials";
req.ContentLength = credentials.Length;
using (var dataStream = req.GetRequestStream())
{
  dataStream.Write(credentials, 0, credentials.Length);
}

asmx Web サービスでは、ユーザー名のみ、または要求から取得したユーザー名とパスワードの組み合わせでログインできます。

Sitecore.Security.Authentication.AuthenticationManager.Login(userName);

編集:資格情報をプレーンテキストとして送信する場合、セキュリティ上のリスクがあります。少なくとも HTTPS を使用して、より安全にします。

于 2013-03-05T23:07:17.457 に答える