【发布时间】:2020-09-15 08:05:33
【问题描述】:
我使用 asp .net MVC 创建了登录,并为选择“记住我”选项的用户添加了一个 cookie。下面是用于添加 cookie 的代码
if (model.LoginViewModel.RememberMe)
{
var authTicket = new FormsAuthenticationTicket(
1,
model.LoginViewModel.Email,
DateTime.Now,
DateTime.Now.AddMinutes(20), // expiry
model.LoginViewModel.RememberMe, //true to remember
"",
"/");
//encrypt the ticket and add it to a cookie
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);
}
我也将此配置添加到 web.config 中。
<authentication mode="Forms">
<forms loginUrl="~/candidate" timeout="2880" />
</authentication>
当我要第二次登录时,我仍然看不到我的登录详细信息。
我在这里遗漏了什么还是有其他方法可以做到这一点?
【问题讨论】:
-
您没有为此使用辅助函数是否有特殊原因?
FormsAuthentication.SetAuthCookie(model.LoginViewModel.Email, model.LoginViewModel.RememberMe);将使用您在<forms />部分中分配的值。 -
@TiesonT。我试过 FormsAuthentication.SetAuthCookie(model.LoginViewModel.Email, model.LoginViewModel.RememberMe);之前。但那里没有运气。
-
除非您的网站配置错误,否则没有理由不工作。您确实需要在创建 auth cookie 后发出重定向 - 它会在下一个请求时生效。
-
值得注意的是,如果这是一个新项目,FormsAuthentication 通常会被禁用 - 您可以通过 OWIN 使用另一种基于声明的方法。
-
@TiesonT。你能解释一下使用 OWIN 的方法吗?
标签: asp.net-mvc authentication cookies remember-me