Why is my ASP.NET MVC persistent auth cookie not working?

前端 未结 2 1706
南方客
南方客 2020-12-18 21:22

I\'m using ASP.NET MVC 3, with forms authentication (based on modified vanilla account code you get with file->new).

When you login, I am setting an auth cookie with

相关标签:
2条回答
  • 2020-12-18 21:40

    Solved from comment from @alexl:

    you can check this answer: Making user login persistant with ASP .Net Membership

    Ok, this link seemed to sort it for me - sticking with SetAuthCookie and tweaking my config to explicitly set the cookie name (in the web.confg), and all is working now. Weird! –

    0 讨论(0)
  • 2020-12-18 21:42

    I'd better create myself a cookie using authentication ticket. SetAuthCookie creates an auth ticket under the hood. Have you tried making your own auth ticket? It will let you store extra data on it.

    Here's an example :

    // create encryption cookie         
    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, 
            userName, 
            DateTime.Now,
            DateTime.Now.AddDays(90),
            createPersistentCookie, 
            string.Empty);
    
    // add cookie to response stream         
    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);    
    System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
    if (authTicket.IsPersistent) 
    {     
          authCookie.Expires = authTicket.Expiration; 
    }
    System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);  
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题