3 回答
TA贡献1860条经验 获得超9个赞
回答有关为此帖子 投下反对票的最新信息,创建具有用户信息的cookie的方法如下,
登录页面页面加载时的Cookie验证,
if (HttpContext.Current.User.Identity.IsAuthenticated)
经过身份验证的用户登录期间创建Cookie,
FormsAuthentication.SetAuthCookie(txtUserName.Text.Trim(), true);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
txtUserName.Text.Trim(),
DateTime.Now,
(chkRemember.Checked) ? DateTime.Now.AddHours(6) : DateTime.Now.AddHours(2),// Specify timelimit as required
true,
string.Empty,
FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.Expires = (chkRemember.Checked) ? DateTime.Now.AddHours(6) : DateTime.Now.AddHours(2);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
以下是投反对票的答案 -在Cookie中添加加密密码的原因。
另一种创建cookie的方式,
HttpCookie toolCookie = new HttpCookie("xyz");
toolCookie["UserName"] = userName;
toolCookie["Password"] = StringCipher.Encrypt(password, "#!");
toolCookie.Expires = DateTime.Now.AddMinutes(chkRemember.Checked ? 30 : -30);
Request.Cookies.Add(toolCookie);
参考
获取现有的Cookie详细信息
HttpCookie user = Request.Cookies["xyz"];
if(user != null)
{
string username = user["UserName"];
string password = user["Password"] != null ? StringCipher.Decrypt(user["Password"], "#!")
}
这里的数据安全性是一个静态类。
加密和解密功能加密和解密
TA贡献1812条经验 获得超5个赞
这是很大的帮助。我要做的唯一更改是从web.config获取expiryDate超时时间。将DateTime.Now.AddMinutes(30)更改为DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes)
- 3 回答
- 0 关注
- 540 浏览
添加回答
举报