票证

//登录认证
public void LoginAuthentication(string UserName, string passWord, string rememberPassowrd)
{
string userData = rememberPassowrd + "|" + passWord;
System.Web.Security.FormsAuthenticationTicket tk = new System.Web.Security.FormsAuthenticationTicket(
1, //指定版本号:可随意指定
UserName,
System.DateTime.Now, //发布时间
System.DateTime.Now.AddDays(7), //失效时间
false, //是否为持久 Cookie:尚未发现有何用,至少目前偶还不知,下面会有说明
userData//用户数据:可用 ((System.Web.Security.FormsIdentity)User.Identity).Ticket.UserData 获取
);
string str = System.Web.Security.FormsAuthentication.Encrypt(tk);//加密身份验票

//声明一个 Cookie,名称为 Web.config 中 <forms name=".APSX" … /> 的 name 属性,对应的值为身份验票加密后的字串
System.Web.HttpCookie ck = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, str);

//指定 Cookie 为 Web.config 中 <forms path="/" … /> path 属性,不指定则默认为“/”
ck.Path = System.Web.Security.FormsAuthentication.FormsCookiePath;
//此句非常重要,少了的话,就算此 Cookie 在身份验票中指定为持久性 Cookie ,也只是即时型的 Cookie 关闭浏览器后就失效;因此上面我说:我是真的还不知在身份验票中指定为持久性 Cookie 有何用。
if (rememberPassowrd.ToLower() == "true")
{
ck.Expires = System.DateTime.Now.AddDays(7);
}

Response.Cookies.Add(ck);//添加至客房端
}

posted @ 2016-03-31 10:42  M i S s  阅读(186)  评论(0编辑  收藏  举报