3 回答

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊
回答有關(guān)為此帖子 投下反對票的最新信息,創(chuàng)建具有用戶信息的cookie的方法如下,
登錄頁面頁面加載時的Cookie驗證,
if (HttpContext.Current.User.Identity.IsAuthenticated)
經(jīng)過身份驗證的用戶登錄期間創(chuàng)建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中添加加密密碼的原因。
另一種創(chuàng)建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);
參考
獲取現(xiàn)有的Cookie詳細(xì)信息
HttpCookie user = Request.Cookies["xyz"];
if(user != null)
{
string username = user["UserName"];
string password = user["Password"] != null ? StringCipher.Decrypt(user["Password"], "#!")
}
這里的數(shù)據(jù)安全性是一個靜態(tài)類。
加密和解密功能加密和解密

TA貢獻(xiàn)1812條經(jīng)驗 獲得超5個贊
這是很大的幫助。我要做的唯一更改是從web.config獲取expiryDate超時時間。將DateTime.Now.AddMinutes(30)更改為DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes)
- 3 回答
- 0 關(guān)注
- 564 瀏覽
添加回答
舉報