2017-07-28 57 views
0

我在登录操作方法来设置AuthCookie这样如何在刷新页面刷新时扩展AuthCookie过期时间?

FormsAuthentication.SetAuthCookie(username,true); 

,并在Global.asax文件我把刷新代码,(这将每个操作方法进行认证后才能执行)

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) 
{ 
string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name; 

HttpCookie cookie = FormsAuthentication.GetAuthCookie(username, true); 
var ticket = FormsAuthentication.Decrypt(cookie.Value); 
var newticket = new FormsAuthenticationTicket(ticket.Version,                  
ticket.Name,ticket.IssueDate,          
ticket.Expiration,true,"new user data",ticket.CookiePath); 

cookie.Value = FormsAuthentication.Encrypt(newticket); 
cookie.Expires = newticket.Expiration.AddHours(24); 
HttpContext.Current.Response.Cookies.Set(cookie); 
} 

但它不起作用,并在一小段时间后注销,但当我通过浏览器设置检查该cookie时,过期时间设置正确。

回答

0

有可能是“对登录用户/会话的Web.config中超时设置的值,这样的事情:

<authentication mode="Forms"> 
    <forms loginUrl="~/User/Login" timeout="10" /> 
</authentication> 

你可以从这里获得关于形式的会议一些有用的信息: Forms authentication timeout vs sessionState timeout