Question

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?

Was it helpful?

Solution

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;
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top