string secretData = "secret";
DateTime dt = DateTime.Now;
FormsAuthenticationTicket newTicket =
new FormsAuthenticationTicket(1, "ticketName", dt,
dt.AddDays(7), true, secretData);
HttpCOOKIE newUserCOOKIE = new HttpCOOKIE("theCOOKIE");
newUserCOOKIE.Value = FormsAuthentication.Encrypt(newTicket);
newUserCOOKIE.Expires.AddDays(7);
Response.COOKIEs.Add(newUserCOOKIE);
用法:
HttpCOOKIE userCOOKIE = Request.COOKIEs["theCOOKIE"]; string secretData = null; if(userCOOKIE != null) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(userCOOKIE.Value); secretData = ticket.UserData; }