if (FormsAuthentication.Authenticate(UserName.Value, UserPassword.Value))
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (UserName.Value, false, 5000);
//将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。第一参数:经过验证的用户名 第二个参数若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false。
FormsAuthentication.RedirectFromLoginPage(UserName.Value,Persist.Checked);
}
if (Membership.ValidateUser(username, password))
{
string userData = "ApplicationSpecific data for this user.";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
DateTime.Now.AddMinutes(30),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
// 返回导致重定向到登录页的原始请求的重定向 URL
Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));
}
FormsAuthentication.SetAuthCookie (String, Boolean, String) 方法为提供的用户名创建一个身份验证票证,并使用提供的 Cookie 路径或 URL 将其添加到响应的 Cookie 集合。参数1: 已验证的用户的名称。参数2: 若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false。参数3: Forms 身份验证票证的 Cookie 路径。
public static void SetAuthCookie(string userName,bool createPersistentCookie, string strCookiePath)
FormsAuthentication的GetAuthCookie 方法 (String, Boolean, String) 为给定的用户名创建身份验证 Cookie。返回一个 HttpCookie,其中包含加密的 Forms 身份验证票证信息。
public static HttpCookie GetAuthCookie(string userName,bool createPersistentCookie,string strCookiePath)
FormsAuthentication..::.FormsCookieName 属性获取用于存储 Forms 身份验证票证的 Cookie 名称。用于存储 Forms 身份验证票证的 Cookie 名称。默认值是“.ASPXAUTH”。
FormsCookieName 属性值是在 ASP.NET 应用程序的配置文件中使用 forms 配置元素的 name 属性设置的。FormsCookieName 用于引用存储 FormsAuthenticationTicket 信息的 Cookie。
FormsAuthenticationTicket 类提供对票证的属性和值的访问,这些票证用于 Forms 身份验证对用户进行标识。无法继承。
public FormsAuthenticationTicket(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePath) version:票证的版本号。name:与票证关联的用户名。issueDate:票证发出时的本地日期和时间。expiration:票证过期时的本地日期和时间。isPersistent:如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。userData:存储在票证中的用户特定的数据。cookiePath:票证存储在 Cookie 中时的路径。