web版本的用户登陆票据 FormsAuthenticationTicket

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "username", DateTime.Now, DateTime.Now.AddDays(365),
true, string.Format("{0}:{1}", "username", "password"), FormsAuthentication.FormsCookiePath);
//这边的 string.Format("{0}:{1}", "username", "password"),也可以改为任何值,如密码或IP
string ticString = FormsAuthentication.Encrypt(ticket);
//将加密后的票据保存为cookie
HttpCookie coo = new HttpCookie(FormsAuthentication.FormsCookieName, ticString);
//这里的IsPersistent不会自己判断,需要手工判断,但FormsAuthenticationTicket的expiration的值是有过期性的,无论cookie的Expires设置多久,只要expiration到期,即使cookie存在用户验证也将失败
if (ticket.IsPersistent)
{
coo.Expires = ticket.Expiration;
}
//使用加入了userdata的新cookie
Response.Cookies.Add(coo);
//FormsAuthentication.SetAuthCookie("username", true);//这种是快速写法,用了这种写法就可以不用自己建ticket票据,也无法使用UserData等参数了


//FormsAuthentication.SignOut//用来清除这个Cookie标记//FormsAuthentication.RedirectFromLoginPage(userID, createPersistentCookie);

// <authentication mode="Forms">
// <forms name=".MyCookie" loginUrl="Login.aspx" protection="All" timeout="60"/>
// </authentication>

上一篇:linux系统安全加固


下一篇:C#-使用javascript的FormsAuthentication.SignOut()