ASP.NET的身份验证方式

1,Forms身份验证:

  

验证方法

我们可以使用下面 4 种方法中的一种进行票证写入和重定向操作,其实前 3 种只不过是对第 4 种方法的封装而已。推荐使用 1、4。注意后三种方法不支持cookieless="UseUri"。

// 1. 使用缺省身份验证票证
FormsAuthentication.RedirectFromLoginPage("username", true);
// 2. 使用缺省身份验证票证
FormsAuthentication.SetAuthCookie("username", false);Response.Redirect(FormsAuthentication.GetRedirectUrl("username", false));
// 3. 使用缺省身份验证票证
Response.Cookies.Add(FormsAuthentication.GetAuthCookie("username", false));
Response.Redirect(FormsAuthentication.GetRedirectUrl("username", false));
// 4. 使用自定义身份验证票证
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "username", DateTime.Now, DateTime.Now.AddMinutes(10), false, null);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)));
Response.Redirect(FormsAuthentication.GetRedirectUrl("username", false));