我使用FormsAuthenticationTicket,加密密码并将其存储到会话值,当我检索密码时,我无法解密密码。
加密类似于下面
string pw="xyz";
FormsAuthenticationTicket ticketpw = new FormsAuthenticationTicket(pw, true, 1000);
string securepw = FormsAuthentication.Encrypt(ticketpw);
Session["password"] = securepw;我试图像下面那样解密
Try 1
FormsAuthenticationTicket ticketuname = new FormsAuthenticationTicket(pw, true, 1000);
string secureuname = FormsAuthentication.Decrypt(pw);
Session["password"] = securepw;Try 2
string securepw=FormsAuthentication.Decrypt(pw);
Session["password"] = securepw;错误-无法将FormAuthenticationTicket转换为字符串
发布于 2016-12-23 09:58:41
因为您创建的新票证不同于票证,所以它是加密的。最佳实践是将其放入HttpCookie中,然后检索它:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
DateTime.Now.AddMinutes(30),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));解密:
var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null) return;
var cookieValue = authCookie.Value;
if (String.IsNullOrWhiteSpace(cookieValue)) return;
var ticket = FormsAuthentication.Decrypt(cookieValue)https://stackoverflow.com/questions/41298679
复制相似问题