문제

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