سؤال

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