2

我有一个支持“记住我”选项的登录控件。
标记

<asp:Login ID="UsersLogin" runat="server" OnAuthenticate="UsersLogin_Authenticate" DisplayRememberMe="true"
            CssClass="usersLogin" FailureText="The username or password you supplied is incorrect"
            Width="100%">
</asp:Login>

代码隐藏:

protected void UsersLogin_Authenticate(object sender, AuthenticateEventArgs e)
{
    Page.Validate(); // Executes the validation controls against the input controls.
    if (!Page.IsValid) return; // Return from the function, incase validation fails.
    e.Authenticated = Membership.ValidateUser(UsersLogin.UserName, UsersLogin.Password);
    FormsAuthentication.RedirectFromLoginPage(UsersLogin.UserName, UsersLogin.RememberMeSet);
}

执行的步骤和我的观察(可能不正确):
1)。用户输入凭据并选中“记住我”复选框。ASP.NET 将在客户端创建一个持久 cookie。
2)。然后,ASP.NET 将用户导航到由“returnUrl”查询字符串参数指示的某个受保护资源(如果可用)。否则,它会将用户重定向到 web.config 中标记的“DefaultUrl”属性中指定的页面。
3)。用户关闭浏览器。
4)。用户重新打开浏览器,并键入同一受保护资源的 url。由于持久性 cookie,ASP.NET 将授予对该受保护资源的访问权限。
5)。现在用户导航到登录页面。ASP.NET 应填充用户名和密码文本框,并检查“

第5步是我卡住的地方。我不确定 ASP.NET 是否有责任确保第 5 步。
我知道这个问题已经被问过很多次,但我还没有找到合理的解决方案,因为很多只是变通方法。一些解决方案建议创建您自己的 FormsAuthenticationTicket 并添加 UserData 来存储记住我的状态。Rest 建议在 web.config 中明确写下标签,并明确指定每个参数,包括超时、路径等。

还要纠正我的这句话:“如果我们从应用程序中注销,ASP.NET 将删除持久性 cookie,无论是通过单击“LoginStatus”控件还是通过使用普通链接按钮并 "FormsAuthentication.Log(); FormsAuthentication.RedirectToLoginPage()"
在单击普通链接按钮时进行写入”。

请建议,需要做什么才能使登录控制检查记住我检查。提前致谢。

4

1 回答 1

1

我想我已经找到了解决方案。这就是我为选中复选框所做的操作。

if (User.Identity.IsAuthenticated)
    {
        FormsIdentity identity = User.Identity as FormsIdentity;
        FormsAuthenticationTicket ticket = identity.Ticket;
        if (ticket.IsPersistent)
        {
            UsersLogin.RememberMeSet = true;                    
        }            
    }

如果用户选中了记住我复选框,则该票证将是永久票证。

无论如何谢谢。

于 2012-05-29T09:12:50.370 回答