C# FormsAuthentication赢得';t在默认MVC4 Web应用程序模板中保存用户数据
我已经为此奋斗了一段时间。在VS 2012中,我使用“Internet应用程序”项目模板创建了一个新的MVC4应用程序(为了简单起见,我在使用ExtendedMembershipProvider的常规应用程序中也发现了这个问题) 登录时,我想将一些用户数据放入表单身份验证cookie中,因此我使用以下代码:C# FormsAuthentication赢得';t在默认MVC4 Web应用程序模板中保存用户数据,c#,asp.net-mvc,asp.net-mvc-4,forms-authentication,user-data,C#,Asp.net Mvc,Asp.net Mvc 4,Forms Authentication,User Data,我已经为此奋斗了一段时间。在VS 2012中,我使用“Internet应用程序”项目模板创建了一个新的MVC4应用程序(为了简单起见,我在使用ExtendedMembershipProvider的常规应用程序中也发现了这个问题) 登录时,我想将一些用户数据放入表单身份验证cookie中,因此我使用以下代码: public ActionResult Login(LoginModel model, string returnUrl) { Request.Cookies.Remove(Form
public ActionResult Login(LoginModel model, string returnUrl)
{
Request.Cookies.Remove(FormsAuthentication.FormsCookieName);
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
HttpCookie authCookie = FormsAuthentication.GetAuthCookie(model.UserName, true);
string userData = "This is some test data.";
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
FormsAuthenticationTicket newAuthTicket = new FormsAuthenticationTicket(authTicket.Version, authTicket.Name, authTicket.IssueDate, authTicket.Expiration, authTicket.IsPersistent, userData);
string newAuthTicketEncrypted = FormsAuthentication.Encrypt(newAuthTicket);
authCookie.Value = newAuthTicketEncrypted;
Request.Cookies.Set(authCookie);
// Response.Write("Encrypted cookie value: " + authCookie.Value); // value here differs than what browser sees
// Response.Write("UserData: " + FormsAuthentication.Decrypt(authCookie.Value).UserData + "<br/>"); // userdata is present here.
// return, shortened for brevity
}
}
公共操作结果登录(LoginModel模型,字符串返回URL)
{
Request.Cookies.Remove(FormsAuthentication.formscookeName);
if(ModelState.IsValid&&WebSecurity.Login(model.UserName、model.Password、persistCookie:model.RememberMe))
{
HttpCookie authCookie=FormsAuthentication.GetAuthCookie(model.UserName,true);
string userData=“这是一些测试数据。”;
FormsAuthenticationTicket authTicket=FormsAuthentication.Decrypt(authCookie.Value);
FormsAuthenticationTicket newAuthTicket=新的FormsAuthTicket(authTicket.Version,authTicket.Name,authTicket.IssueDate,authTicket.Expiration,authTicket.IsPersistent,userData);
字符串newAuthTicketEncrypted=FormsAuthentication.Encrypt(newAuthTicket);
authCookie.Value=newAuthTicketEncrypted;
Request.Cookies.Set(authCookie);
//Response.Write(“加密的cookie值:+authCookie.value);//此处的值与浏览器看到的值不同
//Response.Write(“UserData:+FormsAuthentication.Decrypt(authCookie.Value).UserData+”“;//此处存在UserData。 //返回,为简洁而缩短 } }
非常基本。但是,当我解密它时,它不存在于cookie中。问题似乎是有什么东西正在管道的其他地方创建一个新的表单身份验证cookie。我可以通过打印加密cookie的值,并将其与登录请求后出现在浏览器中的值进行比较来证明这一点。他们是不同的!有人正在重新创建cookie并对其进行加密,但用户数据不存在。名称值出现在cookie中-知道在哪里或做什么吗?MS是否使用新的WebMatrix方法中断表单身份验证中的用户数据?您正在对请求设置cookie,您需要在响应对象上设置cookie 嗨!!!你最终成功了吗?哇-真不敢相信我没看到。愚蠢的错误。