0

wcf rest サービスを使用する asp mvc アプリケーションがあります (すべて同じボックスにあります)。認証呼び出しのために、wcf rest サービス内に Cookie を設定しようとしています。

クライアント側のコード -

        HttpResponseMessage resp;
        HttpClient client = new HttpClient("http://localhost/auth/login/");
        resp = client.Get();

Web サービスでは、FormsAuthentication を使用して authcookie を設定するだけです。

        HttpCookie authCookie = FormsAuthentication.GetAuthCookie("foo", false);
        HttpContext.Current.Response.Cookies.Add(authCookie);

資格情報がコードにハードコードされていると仮定すると、ブラウザページに物理的に移動した場合

       http://localhost/auth/login 

(コード内のハード コード資格情報) 認証 Cookie が設定されていることがわかります。ただし、(上記のように) コードから呼び出すだけでは、認証 Cookie は設定されません。

私がここで見落としている明らかなものはありますか?

4

1 に答える 1

1

プログラムで WCF サービスを呼び出すと、設定された Cookie が HttpClient インスタンスに格納されます。クライアント ブラウザには表示されないため、内部に設定されることはありません。したがって、手動で設定する必要があります。

using (var client = new HttpClient("http://localhost/auth/login/"))
{
    var resp = client.Get();
    // Once you get the response from the remote service loop
    // through all the cookies and append them to the response
    // so that they are stored within the client browser.
    // In this collection you will get all Set-Cookie headers
    // sent by the service, so find the one you need and set it:
    foreach (var cookie in result.Headers.SetCookie)
    {
        // the name of the cookie must match the name of the authentication
        // cookie as you set it in your web.config.
        var data = cookie["SomeCookieName"];
        Response.AppendCookie(new HttpCookie("SomeCookieName", data));
    }
}
于 2011-03-28T20:06:15.010 に答える