0
私は私のasp.netウェブサイトの私の覚え書きを実装しようとします。Asp.net - 私を覚えている - ユーザーは常に問題を認証
シナリオ: ユーザーは「remember me」チェックボックスを選択せずにログインします。 彼はログアウトしたり、タブ/ブラウザを閉じます。
問題: ユーザーがログインページに戻ると、彼は「私を覚えている」にチェックしたように接続されているように見えます。
これはLogin.aspx.csのコードです:
protected void Page_Load(object sender, EventArgs e)
{
myCookies = Request.Cookies;
HttpCookie authCookie = myCookies.Get(FormsAuthentication.FormsCookieName);
if (authCookie != null) // Always true!
{
HttpContext page = HttpContext.Current;
System.Security.Principal.IIdentity identity = page.User.Identity;
if (identity.IsAuthenticated) // Always true!
{
//do some data loading
Response.Redirect("~/default.aspx?p=Profil");
}
}
else if (!Page.IsPostBack)
{
Session.Abandon();
}
}
protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
{
if (userCredentialValid())
{
HttpCookie formAuthCook;
Response.Cookies.Clear();
if (LoginUser.RememberMeSet)
{
DateTime expiryDate = DateTime.Now.AddDays(30);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, LoginUser.UserName, DateTime.Now, expiryDate, true, String.Empty);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
formAuthCook = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
formAuthCook.Expires = ticket.Expiration;
formAuthCook.HttpOnly = true;
formAuthCook.Secure = true;
Response.Cookies.Add(formAuthCook);
}
else
{
formAuthCook = mesCookies.Get(FormsAuthentication.FormsCookieName);
if (formAuthCook != null)
{
formAuthCook.Expires = DateTime.Now;
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
}
}
e.Authenticated = true;
}
else
{
e.Authenticated = false;
}
}
おかげで事前に!
EDIT:ChromeとFirefoxでテスト済みです。
これはブラウザの問題です。 Chromeには他のブラウザと同様に自動的に機能が有効になっています。サイトの開発者がそうしたくない場合でも、CookieはCookieを保持します。面白い!https://bugs.chromium.org/p/chromium/issues/detail?id=128513 – Igor
しかし問題はFirefoxと同じです... – UnBoug
Firefoxは多くのブラウザと同じ機能を提供します。その "機能"を無効にしてテストして、それが関連しているかどうかを確認することができます。 – Igor