私は 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');