public static Encrypt ( System.Web.Security.FormsAuthenticationTicket ticket ) : string | ||
ticket | System.Web.Security.FormsAuthenticationTicket | |
Résultat | string |
public static string Encrypt (FormsAuthenticationTicket ticket)
{
if (ticket == null)
throw new ArgumentNullException ("ticket");
Initialize ();
byte [] ticket_bytes = ticket.ToByteArray ();
if (protection == FormsProtectionEnum.None)
return Convert.ToBase64String (ticket_bytes);
byte [] result = null;
MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetWebApplicationSection (machineKeyConfigPath);
if (protection == FormsProtectionEnum.All) {
result = MachineKeySectionUtils.EncryptSign (config, ticket_bytes);
} else if (protection == FormsProtectionEnum.Encryption) {
result = MachineKeySectionUtils.Encrypt (config, ticket_bytes);
} else if (protection == FormsProtectionEnum.Validation) {
result = MachineKeySectionUtils.Sign (config, ticket_bytes);
}
return Convert.ToBase64String (result);
}
protected void Button1_Click(object sender, EventArgs e) { if (txtusername.Text == "admin" && txtpwd.Text == "admin") { //会话性cookie保存于内存中。关闭浏览器则会话性cookie会过期消失;持久化cookie则不会,直至过期时间已到或确认注销。 FA.SetAuthCookie(txtusername.Text, true); #region 票据验证 string userData = "序列化对象"; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtusername.Text, DateTime.Now, DateTime.Now.AddMinutes(720), true, userData, FA.FormsCookiePath); // 加密票证 string encTicket = FA.Encrypt(ticket); // 创建cookie HttpCookie cookie = new HttpCookie(FA.FormsCookieName, encTicket); cookie.HttpOnly = true; cookie.Secure = FA.RequireSSL; cookie.Domain = FA.CookieDomain; cookie.Path = FA.FormsCookiePath; if (ticket.IsPersistent) { cookie.Expires = DateTime.Now.AddMinutes(720); } HttpContext.Current.Response.Cookies.Add(cookie); #endregion Response.Redirect("admin/index.aspx"); } else { Response.Write("用户名或密码错误"); } }