Forms Authentication cookie not expiring

笑着哭i 提交于 2019-12-03 20:32:51

Almost a month, 100 views and no answers after I have found a solution.

First, the timeout specified in the web.config works only when the cookie is set as persistent i.e. a persistent cookie can also expire. Initially I wrongly assumed that a persistent cookie can not expire. In fact, my original code would have worked if I had always set the cookie to persistent.

Secondly, I believe there is no need for a membership provider to make Forms Authentication work as suggested in the comments above.

Here is how I now create a Authentication cookie:

HttpCookie authCookie = FormsAuthentication.GetAuthCookie(username, isPersistent);
if (!isPersistent)
{
    //this is because if it was not set then it got 
    //automatically set to expire next year even if 
    //the cookie was not set as persistent
    authCookie.Expires = DateTime.Now.AddMinutes(15);
}

Response.Cookies.Add(authCookie); 

Please let me know if there is any alternate to this?

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