asp.net登录控制 - 设置用户身份验证票证到期日期

时间:2011-07-20 10:58:39

标签: asp.net authentication

我想在login.aspx上覆盖用户身份验证票证的到期日期。

此代码无法正常运行,因为1分钟后用户仍会进行身份验证。

private int loginExpire = 1;
protected void Login_LoggedIn(object sender, EventArgs e)
{
   HttpCookie authCookie = Response.Cookies[FormsAuthentication.FormsCookieName];
   FormsAuthenticationTicket oldAuthTicket = FormsAuthentication.Decrypt(authCookie.Value);

  var newAuthTicket = new FormsAuthenticationTicket(
                oldAuthTicket.Version, 
                oldAuthTicket.Name, 
                DateTime.Now, 
                DateTime.Now.Add               
               (TimeSpan.FromMinutes(loginExpire)), 
               oldAuthTicket.IsPersistent,       
               oldAuthTicket.UserData, 
               FormsAuthentication.FormsCookiePath);



 string encryptedTicket = FormsAuthentication.Encrypt(newAuthTicket);
 authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
 HttpContext.Current.Response.Cookies.Set(authCookie);

 FormsAuthentication.RedirectFromLoginPage(GetDestinationPage(lgUserLogin.UserName), false);
}

的web.config

 <authentication mode="Forms">
   <forms loginUrl="~/Account/Login.aspx" requireSSL="false" timeout="1"         slidingExpiration="true" protection="All"/>
 </authentication>

1 个答案:

答案 0 :(得分:1)

在web.config中编辑身份验证的表单元素部分:set timeout =“1”和slidingExpiration =“false” 或者使用以下代码代替RedirectFromLoginPage方法:

String returnUrl;
if (Request.QueryString["ReturnURL"] == null)
{
    returnUrl = "/Default.aspx"; //your default page url
}
else
{
    returnUrl = Request.QueryString["ReturnURL"];
}
Response.Redirect(returnUrl);