質問

Using Forms Authentication to secure my WCF service.

After validating the user, I try to set Forms Authentication Cookie using the code below:

public bool Login(string username, string password)
        {
            if (Membership.ValidateUser(username, password))
            {
                FormsAuthentication.SetAuthCookie(username, true);
                // what should I do here?
                return true;
            }

            return false;
        }

After the SetAuthCookie how exactly should I check if the user is authenticated? (Whenever I try to access HttpContext.Current.User.Identity.IsAuthenticated, it gives me the value false.)

Any ideas what I am missing here?

役に立ちましたか?

解決

Have you returned the cookie in the response in the login WCF service. In your case you can use this code

public bool Login(string username, string password)
{
   if (Membership.ValidateUser(username, password))
   {
       //FormsAuthentication.SetAuthCookie(username, true);
       // what should I do here?
       HttpCookie v_Cookie = FormsAuthentication.GetAuthCookie (username,true)
       HttpContext.Current.Response.Cookies.Add(v_Cookie);
       return true;
   }

   return false;
}

他のヒント

set the HttpContext.Current.User in global.asax Application_AuthenticateRequest function
in this function,read cookie and create a Principal by cookie value,set it to HttpContext.Current.User

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top