问题
What is this cookie called when it gets set? Can I see it in my cookie folder for my browser?
I think it doesn't get set so my login verification fails.
回答1:
What is this cookie called when it gets set?
The default name for the cookie is .ASPXAUTH
. It's configurable through the web.config to have any name.
<authentication mode="Forms">
<forms name=".SomeName" loginUrl="Login.aspx" />
</authentication>
So check there for anything non-standard.
Can I see it in my cookie folder for my browser?
You should be able to see it using any browser tools.
回答2:
Normally, if you create Authentication Cookie yourself, you will need to create Principal object and save it inside Current Thread in AuthenticateRequest event for every request.
Otherwise, when you check User.Identity.IsAuthenticated, it will return false.
Global.asax.cs
public class Global : HttpApplication
{
private void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie decryptedCookie =
Context.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(decryptedCookie.Value);
var identity = new GenericIdentity(ticket.Name);
var principal = new GenericPrincipal(identity, null);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = HttpContext.Current.User;
}
}
web.config
Make sure you have authentication tag in web.config. For example,
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880"/>
</authentication>
Usage
public ActionResult Index()
{
var username = User.Identity.Name;
return View();
}
Browser Plugin
I use EditThisCookie Chrome Plugin.
来源:https://stackoverflow.com/questions/34755093/formsauthentication-setauthcookieuser-username-true-cookie-name