1
在我的登錄頁面上,FormsAuthenticationTicket
被設置爲具有一些自定義用戶數據的永久性cookie。現在我需要更改此自定義用戶數據,因此它還包含一個參數。當以前登錄的用戶下次訪問該站點時,我反序列化了此自定義用戶數據Application_PostAuthenticateRequest
,但現在我不會包含新添加的參數。續訂FormsAuthenticationTicket用戶數據
我可以檢索這個參數,而不需要用戶再次登錄嗎?
如果不是,如何告訴持久性cookie它需要'更新'?
Cookie set。 userId是一個新參數:
CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
serializeModel.FirstName = model.UserName;
serializeModel.userId = model.userId;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string userData = serializer.Serialize(serializeModel);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
model.UserName,
DateTime.Now,
DateTime.Now.AddYears(5),
model.RememberMe,
userData);
string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
faCookie.Expires = authTicket.Expiration;
Response.Cookies.Add(faCookie);
return RedirectToAction("Index", "Home");
Cookie檢索。用戶ID是新的參數,則是null
對於以前登錄的用戶:
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
JavaScriptSerializer serializer = new JavaScriptSerializer();
CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);
CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
newUser.FirstName = serializeModel.FirstName;
newUser.userId = serializeModel.userId;
HttpContext.Current.User = newUser;
}
這是MVC。它獲得了正確的Custom Principal,但是因爲它只是反序列化保存在cookie中的一些數據,所以新添加的類型沒有在序列化中設置...因此,用戶應該再次登錄,或者我想知道如何更新。 – perene
您已經檢查過,在加密之前,已經正確設置了新的UserId並正確序列化了UserData。 –
... UserId尚未設置 - 這是整個問題。爲了設置它,用戶需要註銷並重新登錄並創建一個新的cookie,我正在尋找一種不這樣做的方法。 – perene