2016-10-21 21 views
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でテスト済みです。

+0

これはブラウザの問題です。 Chromeには他のブラウザと同様に自動的に機能が有効になっています。サイトの開発者がそうしたくない場合でも、CookieはCookieを保持します。面白い!https://bugs.chromium.org/p/chromium/issues/detail?id=128513 – Igor

+0

しかし問題はFirefoxと同じです... – UnBoug

+0

Firefoxは多くのブラウザと同じ機能を提供します。その "機能"を無効にしてテストして、それが関連しているかどうかを確認することができます。 – Igor

答えて

0

私は回避策を見つけたと思います!

私はセッションが永続的である時に指定するためのクッキーを作成します。

protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e) 
{ 
    if (userCredentialValid()) 
    { 
     HttpCookie formAuthCook; 
     Response.Cookies.Clear(); 

     if (LoginUser.RememberMeSet) 
     { 
      Response.Cookies.Clear(); 
      //set the new expiry date – to thirty days from now 
      DateTime expiryDate = DateTime.Now.AddDays(1); 
      //create a new forms auth ticket 
      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, LoginUser.UserName, DateTime.Now, expiryDate, true, String.Empty); 
      //encrypt the ticket 
      string encryptedTicket = FormsAuthentication.Encrypt(ticket); 
      //create a new authentication cookie – and set its expiration date 
      formAuthCook = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 
      formAuthCook.Expires = ticket.Expiration; 
      formAuthCook.HttpOnly = true; 
      formAuthCook.Secure = true; 

      //######################### EDIT ################################# 
      HttpCookie sCookPersistent = new HttpCookie("pSession"); //In my code I use a constante for cookie name 
      sCookPersistent.Value = "1"; 
      sCookPersistent.HttpOnly = true; 
      sCookPersistent.Secure = true; 
      sCookPersistent.Expires = ticket.Expiration; 
      ////######################### EDIT END ########################## 

      //add the cookie to the response. 
      Response.Cookies.Add(formAuthCook); 
      Response.Cookies.Add(sCookPersistent);//##### EDIT 
     } 
     else 
     { 
      formAuthCook = mesCookies.Get(FormsAuthentication.FormsCookieName); 
      if (formAuthCook != null) 
      { 
       formAuthCook.Expires = DateTime.Now; 
       Response.Cookies.Remove(FormsAuthentication.FormsCookieName); 
      } 

      //######################### EDIT ################################# 
      if (Request.Cookies["pSession"] != null) 
      { 
       Request.Cookies["pSession"].Expires = DateTime.Now; 
       Response.Cookies.Remove("pSession"); 
      } 
      ////######################### EDIT END ########################### 
     } 

     e.Authenticated = true; 
    } 
    else 
    { 
     e.Authenticated = false; 
    } 
} 

そして、ページの読み込みに:

HttpCookieCollection myCookies = Request.Cookies;    
HttpCookie authCookie = myCookies.Get(FormsAuthentication.FormsCookieName); 
bool authCookieValid = false; 

if (authCookie != null) 
{ 
    //Cookie created if the user checked the remember me checkbox 
    //if null the session is not persistent 
    if (Request.Cookies["pSession"] == null) 
    { 
     authCookie.Expires = DateTime.Now; 
     Response.Cookies.Set(authCookie); 
    } 
    else if (!string.IsNullOrEmpty(authCookie.Value)) 
    { 
     authCookieValid = true; 
    } 
} 

それは防弾ではないが、それは今の仕事をしていません。

皆さんにご提案がありましたら、お知らせください。 :)

関連する問題