FormsAuthentication.SetAuthCookie(user.UserName, true); cookie name

旧街凉风 提交于 2020-01-07 03:09:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!