在我们的应用程序中,我们实现了基于角色的表单身份验证。这已使用 RoleModule 处理,我们将角色数据保存在 cookie 中,每次我们从 cookie 中读取数据并实例化IPrincipal对象。此代码在Application_OnPostAcquireRequestState方法中执行:
 HttpApplication application = source as HttpApplication;
 HttpContext context = application.Context;
if (context.User.Identity.IsAuthenticated &&
      (!Roles.CookieRequireSSL || context.Request.IsSecureConnection))
{
//Read the roles data from the Roles cookie..
context.User = new CustomPrincipal(context.User.Identity, cookieValue);
Thread.CurrentPrincipal = context.User;
}
这将初始化context.User对象。每次向服务器发出请求时,都会使用上述流程对用户进行身份验证。在 中Application_EndRequest,我们使用当前的主体对象数据更新角色 cookie。
在我们的页面中有一个FormsAuthentication_OnAuthenticate方法,我们Global.asax在其中读取 cookie,更新 cookie,并在票证过期时更新票证。此外,在此方法中,如果票证过期,我们会尝试在会话对象中设置用户名值。
FormsAuthentication oldTicket = FormsAuthentication.Decrypt(context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
if(oldTicket != null)
{
    if(oldTicket.Expired)
    {
        try
        {
            HttpContext.Current.Session["UserName"] = userName;
        }
        catch
        {
            //Log error.
        }
    FormsAuthentication newTicket =  new FormsAuthenticationTicket(oldTicket.Version, oldTicket.Name, DateTime.Now,
            DateTime.Now.AddMinutes(30), oldTicket.IsPersistent, oldTicket.UserData);
    string encryptedTicket = FormsAuthentication.Encrypt(newTicket);
    HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
    if (isPersistent)
    {
        httpCookie.Expires = DateTime.Now.AddMinutes(300);
    }
}
这是我们在 web.config 中的表单设置:
   <forms defaultUrl="Default.aspx" domain="" loginUrl="Login.aspx" name=".ASPXFORMSAUTH" timeout="20" slidingExpiration="true" />
会话超时为 20 分钟。
问题:如果用户闲置超过 30 分钟(即 FormsAuth 票据续订时间),context.User.Identity.IsAuthenticated则角色模块中的值为false,并context.User设置为NULL。现在,当用户请求一个页面时,他将被重定向到登录页面。但是,cookie 仍然存在。如果用户再次尝试请求页面,则将context.User.IsAuthenticated属性设置为true并将用户带到相应的页面。此外,在FormsAuthentication_OnAuthenticate方法中,当我尝试设置会话值时,它会引发错误,因为会话对象是NULL.
我在这里要实现的是,在持久cookie的情况下,在auth票超时后,用户不应该被注销,即应该重新验证用户。
我怎样才能做到这一点?如果我没有错,设置context.User应该解决目的,但我该怎么做呢?
附加信息:
票证到期后,我尝试请求页面,事件查看器显示错误消息:
Event code: 4005 
Event message: Forms authentication failed for the request. Reason: The ticket supplied has expired. 
Event time: 08-02-2012 20:02:05 
Event time (UTC): 08-02-2012 14:32:05 
Event ID: 048e3238ade94fd6a7289bac36d130ef 
Event sequence: 76 
Event occurrence: 2 
Event detail code: 50202 
Process information: 
Process ID: 21692 
Process name: w3wp.exe 
Account name: IIS APPPOOL\ASP.NET v4.0 Classic 
我使用的是标准机器密钥设置,存储在 web.config 中,而不是自动生成的。此外,我检查了所有错误的进程 ID 及其相同。