2014-10-09 61 views
3

當我登錄時,使用.NET 4和帶有自定義佈局模板的asp.net登錄控件,無論是否選中記住我複選框,控件似乎會創建一個身份驗證Cookie,並讓我登錄,直到我通過單擊退出按鈕明確退出。在登錄狀態下關閉瀏覽器並不會使我註銷。asp:使用LayoutTemplate登錄會創建持久性cookie,無論是否記住我被檢查

有人可以幫助解釋可能是什麼原因造成的?

<asp:Login ID="Login1" runat="server" OnLoggingIn="Login1_LoggingIn" OnLoggedIn="Login1_LoggedIn" OnLoginError="Login1_LoginError"> 
    <LayoutTemplate> 
     <asp:Panel runat="server" DefaultButton="btnLogin"> 
      <label>Email</label>&nbsp;<div class="required">*</div>&nbsp; 
      <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" Display="Dynamic" ErrorMessage="Required" InitialValue="" SetFocusOnError="true" ValidationGroup="Login" /><br /> 
      <asp:TextBox runat="server" ID="UserName" class="input" ValidationGroup="Login" /> 
      <label>Password</label>&nbsp;<div class="required">*</div>&nbsp; 
      <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" Display="Dynamic" ErrorMessage="Required" InitialValue="" SetFocusOnError="true" ValidationGroup="Login" /><br /> 
      <asp:TextBox runat="server" ID="Password" TextMode="Password" class="input" style="margin:0 0 6px 0;" ValidationGroup="Login" /> 
      <asp:Checkbox runat="server" ID="RememberMe" Text="Remember me" CssClass="remember-me" /> 
      <asp:LinkButton runat="server" ID="btnLogin" CommandName="Login" Text="Sign In" CssClass="login-button" ValidationGroup="Login" /> 
     </asp:Panel> 
    </LayoutTemplate> 
</asp:Login> 


protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e) 
{ 
    string username = Login1.UserName.Trim(); 

    if (IsValid) 
    { 
     MembershipUser user1 = Membership.GetUser(username); 
     if (user1 != null) 
     { 
      if (Membership.ValidateUser(user1.UserName, Login1.Password)) 
      { 
       Login1.UserName = user1.UserName; 
      } 
     } 
    } 


protected void Login1_LoggedIn(object sender, EventArgs e) 
{ 
    if (Roles.IsUserInRole(Login1.UserName, "Users")) 
    { 
     Response.Redirect("users.aspx", true); 
    } 

<authentication mode="Forms"> 
    <forms timeout="129600" name=".AUTHCOOKIE" protection="All" slidingExpiration="true" path="/" requireSSL="false" loginUrl="~/login.aspx" cookieless="UseCookies"/> 
</authentication> 

回答

0

找到答案... geez finally!

的Login.aspx:

<asp:Login ID="Login1" runat="server" OnLoggingIn="Login1_LoggingIn"> 
     <LayoutTemplate> 
      <asp:Panel runat="server" DefaultButton="btnLogin"> 
       <label>Email</label>&nbsp;<div class="required">*</div> 
       &nbsp; 
    <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" Display="Dynamic" ErrorMessage="Required" InitialValue="" SetFocusOnError="true" ValidationGroup="Login" /><br /> 
       <asp:TextBox runat="server" ID="UserName" class="input" ValidationGroup="Login" /> 
       <label>Password</label>&nbsp;<div class="required">*</div> 
       &nbsp; 
    <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" Display="Dynamic" ErrorMessage="Required" InitialValue="" SetFocusOnError="true" ValidationGroup="Login" /><br /> 
       <asp:TextBox runat="server" ID="Password" TextMode="Password" class="input" Style="margin: 0 0 6px 0;" ValidationGroup="Login" /> 
       <asp:CheckBox runat="server" ID="RememberMe" Text="Remember me" CssClass="remember-me" /> 
       <asp:LinkButton runat="server" ID="btnLogin" CommandName="Login" Text="Sign In" CssClass="login-button" ValidationGroup="Login" /> 
      </asp:Panel> 
     </LayoutTemplate> 
    </asp:Login> 

Login.aspx.cs

protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e) 
{ 
    if (IsValid) 
    { 
     if (FormsAuthentication.Authenticate(Login1.UserName, Login1.Password)) 
     { 
      FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false); 
     } 
    } 
} 

Web.config文件:

<authentication mode="Forms"> 
    <forms timeout="129600" name=".AUTHCOOKIE" protection="All" slidingExpiration="true" path="/" requireSSL="false" loginUrl="~/login.aspx" cookieless="UseCookies"> 
     <credentials passwordFormat="Clear"> 
      <user name="test" password="test"/> 
     </credentials> 
    </forms> 
</authentication> 

其他注意事項:

在我的web應用程序,我有一個名爲「受保護」和af的文件夾在裏面調用「Users.aspx」和另一個名爲Web.Config的文件。在「保護」文件夾內的web.config中有以下不允許匿名用戶,只允許「test」的用戶:

<configuration> 
    <system.web> 
     <authorization> 
      <deny users="?"/> 
      <allow users="test"/> 
     </authorization> 
    </system.web> 
</configuration> 

我關閉了我所有的Chrome實例登錄後,然後去給用戶.aspx頁面,當然,被要求重新登錄!我知道這段代碼並不完全是你所擁有的,但你應該能夠修改你的代碼來採用這個。

+0

感謝您的回覆,但這是行不通的。我在if(Membership.ValidateUser())中添加了RedirectFromLoginPage(),並且在成功登錄後記住我沒有選中我關閉瀏覽器窗口,然後返回到網站,我仍然登錄。這是在我們的測試服務器上,不是本地主機。 – 2014-10-10 15:37:20

+0

你關閉了整個瀏覽器還是關閉了選項卡?因爲我試圖關閉整個瀏覽器和所有實例,並且它對我有用......嗯......讓我看看我是否可以拿出別的東西。 – 2014-10-10 18:42:19

+0

這是我從那裏得到答案,併爲他工作:http://stackoverflow.com/questions/2715873/asp-net-forms-authentication-cookie-not-expiring/2716020#2716020 – 2014-10-10 18:43:37

相關問題