1.配置文件中设置为Forms验证
<
authentication mode
=
"
Forms
"
>
< forms loginUrl = " NetFromwork/FormLogin.aspx " defaultUrl = " NetFromwork/FormLogin.aspx " name = " .ASPNETFORMSTEST " protection = " All " timeout = " 20 " ></ forms >
</ authentication >
< authorization >
< deny users = " ? " />
</ authorization >
< forms loginUrl = " NetFromwork/FormLogin.aspx " defaultUrl = " NetFromwork/FormLogin.aspx " name = " .ASPNETFORMSTEST " protection = " All " timeout = " 20 " ></ forms >
</ authentication >
< authorization >
< deny users = " ? " />
</ authorization >
2.验证用户合法后,将身份验证票证写入Cookie集合
string
userName
=
"
Jimmy
"
;
// Create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1 , userName, DateTime.Now, DateTime.Now.AddMinutes( 1 ), false , "" );
// Encrypt the ticket.
String encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add( new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
// Redirect back to default or original URL.
FormsAuthentication.RedirectFromLoginPage(userName, true );
// Create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1 , userName, DateTime.Now, DateTime.Now.AddMinutes( 1 ), false , "" );
// Encrypt the ticket.
String encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add( new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
// Redirect back to default or original URL.
FormsAuthentication.RedirectFromLoginPage(userName, true );
上述代码已经实现了简单的Forms身份验证功能。接下来我们看看票据信息构造函数 FormsAuthenticationTicket()和用户重定向函数 RedirectFromLoginPage()的原型:
函数FormsAuthenticationTicket()原型
//
// 摘要:
// 使用 cookie 名、版本、目录路径、发布日期、过期日期、持久性以及用户定义的数据初始化 System.Web.Security.FormsAuthenticationTicket
// 类的新实例。
//
// 参数:
// version:
// 票证的版本号。
//
// name:
// 与身份验证票关联的用户名。
//
// userData:
// 存储在票证中的用户特定的数据。
//
// isPersistent:
// 如果票证将存储在持久性 Cookie(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。
//
// issueDate:
// 票证发出时的本地日期和时间。
//
// cookiePath:
// 票证存储在 Cookie 中时的路径。
//
// expiration:
// 票证过期时的本地日期和时间。
public FormsAuthenticationTicket(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePath);
//
// 摘要:
// 使用 cookie 名、版本、目录路径、发布日期、过期日期、持久性以及用户定义的数据初始化 System.Web.Security.FormsAuthenticationTicket
// 类的新实例。
//
// 参数:
// version:
// 票证的版本号。
//
// name:
// 与身份验证票关联的用户名。
//
// userData:
// 存储在票证中的用户特定的数据。
//
// isPersistent:
// 如果票证将存储在持久性 Cookie(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。
//
// issueDate:
// 票证发出时的本地日期和时间。
//
// cookiePath:
// 票证存储在 Cookie 中时的路径。
//
// expiration:
// 票证过期时的本地日期和时间。
public FormsAuthenticationTicket(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePath);
函数RedirectFromLoginPage()原型
//
// 摘要:
// 将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。
//
// 参数:
// userName:
// 经过身份验证的用户名。
//
// createPersistentCookie:
// 若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false。
public static void RedirectFromLoginPage(string userName, bool createPersistentCookie);
//
// 摘要:
// 将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。
//
// 参数:
// userName:
// 经过身份验证的用户名。
//
// createPersistentCookie:
// 若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false。
public static void RedirectFromLoginPage(string userName, bool createPersistentCookie);
论坛常见的 登陆有效期功能是怎么实现的呢?是通过设置哪些参数实现的呢?不禁有些迷茫,涉及到登陆有效期的参数有如下几个:函构造数 FormsAuthenticationTicket()中的 isPersistent和 expiration,函数 RedirectFromLoginPage()中的 createPersistentCookie,配置文件中还有 timeout属性,究竟要怎么设置才能实现“ 登陆一小时,一天,一个月,永久,浏览器进程”功能呢?
下面谈谈我对这些参数的理解, 通过查找网上资料,MSDN和程序测试而来,有理解错误的地方欢迎大家指正。
1.关于 持久Cookie,并不是说 isPersistent=True或者 createPersistentCookie=True时,就能够保持永久登陆状态,持久Cookie解释成跨浏览器会话保存的Cookie更合适。当设置为True时,不同浏览器进程之间可以共享该Cookie,也就是说在一个IE中登陆后,在另一个IE中打开还保持登陆状态。
2.关于 函数RedirectFromLoginPage(),MSDN上解释是这样的“如果 CookiesSupported 属性为 true,并且 ReturnUrl 变量位于当前应用程序中,或者 EnableCrossAppRedirects 属性为 true,则 RedirectFromLoginPage 方法将发出身份验证票证并使用 SetAuthCookie 方法将其置于默认 Cookie 中。”
可见,在执行函数 RedirectFromLoginPage()时,会覆盖掉 FormsAuthenticationTicket实例的票据信息,所以第二部分代码中,不该调用该函数,而应该用下面的代码代替:
string
userName
=
"
Jimmy
"
;
// Create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1 , userName, DateTime.Now, DateTime.Now.AddMinutes( 1 ), false , "" );
// Encrypt the ticket.
String encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add( new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
// Redirect back to default or original URL.
Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, true )); //代替RedirectFormLoginPage
// Create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1 , userName, DateTime.Now, DateTime.Now.AddMinutes( 1 ), false , "" );
// Encrypt the ticket.
String encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add( new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
// Redirect back to default or original URL.
Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, true )); //代替RedirectFormLoginPage
3.关于 登陆有效时间,经过上面的修改,涉及到登陆时间的只有构造函数 FormsAuthenticationTicket()的参数 expiration票据过期时间和配置文件中的timeout属性了,这两个有什么关系呢? 票据的有效时间是由expiration属性决定的,而timeout只有当expiration属性没有显示设置的时候才生效。
希望能给大家带来一点帮助,分析不对的地方,欢迎大家指正。参考资料: Asp.net中基于Forms验证的角色验证授权
源代码下载地址: 点击下载完整示例程序 (示例路径:/ NetFromwork/FormLogin.aspx )