我正在使用FormsAuthentication,并且无法延长到期日期

时间:2015-10-08 11:26:35

标签: c# asp.net formsauthentication

我正在构建一个Web应用程序(框架4.6),并使用FormsAuthentication来管理安全性。

目前,它允许用户登录/注销等...一切都很好。 但是,我想定期检查表单身份验证故障单的到期时间,并弹出一个对话框,其中包含用户将按下以延长时间的按钮。所以我基本上有它工作,除了更新机票时,用户基本上被踢出去了,我不明白为什么。

这里是登录代码:

FormsAuthenticationTicket faTicket = new FormsAuthenticationTicket(1, user.UserID, DateTime.Now, DateTime.Now.AddMinutes(FormsAuthentication.Timeout.Minutes), persistLogin, "");
string cookiestr = FormsAuthentication.Encrypt(faTicket);
HttpCookie ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
if (persistLogin)
    ck.Expires = faTicket.Expiration;
ck.Path = FormsAuthentication.FormsCookiePath;
HttpContext.Current.Response.Cookies.Add(ck);

以及此处的机票更新代码:

FormsIdentity identity = ((FormsIdentity)HttpContext.Current.User.Identity);
string userID = identity.Name;
HttpCookie cookie = FormsAuthentication.GetAuthCookie(userID, true);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);

FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(
    ticket.Version,
    userID,
    ticket.IssueDate, 
    DateTime.Now.AddMinutes(FormsAuthentication.Timeout.Minutes), 
    ticket.IsPersistent, 
    ticket.UserData, 
    ticket.CookiePath);

cookie.Value = FormsAuthentication.Encrypt(newTicket);
if (ticket.IsPersistent)
    cookie.Expires = newTicket.Expiration;
HttpContext.Current.Response.Cookies.Add(cookie);

顺便提一下,我处理剩余时间的方式如下(在ashx处理程序文件中):

FormsIdentity identity = ((FormsIdentity)HttpContext.Current.User.Identity);
DateTime expires = identity.Ticket.Expiration;

// offset the expiry time by a few seconds, because otherwise the FormsAuthentication will prevent this Handler from executing
expires = expires.AddSeconds(-5);

TimeSpan ts = expires - DateTime.Now;
double mins = ts.Minutes;
double secs = ts.Seconds;
string countdownText = mins.ToString().PadLeft(2, '0') + ":" + secs.ToString().PadLeft(2, '0');

1 个答案:

答案 0 :(得分:0)

由于您要重新发票,因此将发布日期设置为当前日期可能是有意义的 - >

CustomerRoutes.ts