我正在使用表单身份验证,当我想要检索FormsAuthenticationTicket的UserData时,它是一个空字符串。但是,当我设置的UserData是一个长字符串时会发生这种情况,当我设置一个较短的字符串时它运行良好。这是我的代码:
[HttpPost]
public ActionResult Login(USUARIO model, string returnUrl)
{
if (ModelState.IsValid)
{
USUARIO usuario = db.USUARIO.Where(x => x.USUARIO1 == model.USUARIO1 && x.CLAVE == model.CLAVE).FirstOrDefault();
if (usuario != null)
{
FormsAuthentication.SetAuthCookie(usuario.USUARIO1, false);
SetAuthCookie(usuario.USUARIO1, false, usuario);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "El usuario o password es incorrecto.");
}
}
return View(model);
}
public void SetAuthCookie(string userName, bool createPersistentCookie, USUARIO userData)
{
string jsonUserData = getJsonUsuarioSession(userData); //Returns Json string OK
HttpCookie cookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(
ticket.Version,
ticket.Name,
ticket.IssueDate,
ticket.Expiration,
ticket.IsPersistent,
jsonUserData,
ticket.CookiePath
);
string encTicket = FormsAuthentication.Encrypt(newTicket);
cookie.Value = encTicket;
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
我想要检索UserData的方式是:
FormsIdentity id = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
var x = ticket.UserData;
知道为什么会这样吗?提前谢谢!