如何让asp.net登录控件自动验证以前认证的用户?

Mik*_*keJ 3 asp.net authentication cookies controls login

我正在尝试设置登录控件以记住之前已成功输入用户名和密码的用户的登录凭据.我将remember me属性设置为true,但它似乎没有任何可以读取cookie并自动登录用户的事件.

是否有直接的机制来实现这一目标?

Rob*_*son 5

您需要在ASP.NET 2.0中使用 Google进行表单身份验证

您需要设置应用程序(通过web.config),还可能需要更改IIS设置.虽然它很简单,但可以使用大量设置,所以最好阅读一些文章.ScottGu有一篇 博客文章,详细介绍了很多细节.

www.asp.net上还有很多很好的视频,包括这些安全教程

尝试如何:创建ASP.NET登录页面演练:创建具有成员身份和用户登录的网站.如果我记得,除非您使用Sql Server Membership提供程序,否则您仍需要自己进行身份验证.在这种情况下,您仍然需要设置数据库和web.config.


基本上,一旦你正确设置了配置,你就有了一个登录页面.在登录页面告诉窗体身份验证创建身份验证票给你,一旦验证他们的身份:

if (VerifyUser(name, password) ) // this is not a framework method
    FormsAuthentication.RedirectFromLoginPage(
        userName, false); // no persistent cookie
Run Code Online (Sandbox Code Playgroud)

如果要读取身份验证票证数据(来自其他任何位置).

// output just writes to a StringBuilder 'sb' 
output(sb, "Identity.AuthenticationType", Page.User.Identity.AuthenticationType);

FormsIdentity fi = Page.User.Identity as FormsIdentity;
if (fi == null)
{
    output(sb, "Identity Type", Page.User.Identity.ToString());
    return;
}

output(sb, "FormsIdentity.Ticket.IssueDate", fi.Ticket.IssueDate);
output(sb, "FormsIdentity.Ticket.Expiration", fi.Ticket.Expiration);
output(sb, "FormsIdentity.Ticket.Name", fi.Ticket.Name);
output(sb, "FormsIdentity.Ticket.CookiePath", fi.Ticket.CookiePath);
output(sb, "FormsIdentity.Ticket.UserData", fi.Ticket.UserData);
output(sb, "FormsIdentity.Ticket.Version", fi.Ticket.Version);
output(sb, "FormsIdentity.Ticket.IsPersistent", fi.Ticket.IsPersistent);
Run Code Online (Sandbox Code Playgroud)

关键是,一旦通过身份验证,如果身份验证票证已过期且用户位于受保护的页面上,asp.net将仅将用户重定向到登录页面.Asp.net不会一直要求您不必要地对用户进行身份验证.