使用FormsAuthentication.SetAuthCookie(用户名,false)后,(Request.IsAuthenticated)为false

时间:2013-10-09 19:24:42

标签: forms-authentication asp.net-mvc-5

我正在使用VS2013 RC和MVC 5构建一个网站,我正在尝试使用formsAuthentification而不在我的网站上注册永久用户。

我发布到我公司的api来验证用户的名字和密码。当这成功恢复时,我想发布一个授权cookie:

System.Web.Security.FormsAuthentication.SetAuthCookie(username, false);

在调用它之后,我看到.ASPXAUTH = ... cookie。

但是,我无法进入模板的_LoginPartial.cshtml页面上的@if(User.Identity.IsAuthenticated)或者@if(Request.IsAuthenticated)块。

这项技术在MVC 4中对我有用,我试图将其弯曲以适应MVC 5的OWIN身份验证。

4 个答案:

答案 0 :(得分:16)

我需要在web.config中启用表单身份验证

<system.web>
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
...
</system.web>

答案 1 :(得分:2)

如果你不想打击MVC5新的身份验证模式(OWIN),你可以在这个链接中找到你的答案

http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown-part-deux#disqus_thread

答案 2 :(得分:1)

我尝试了上述所有解决方案,但解决我问题的方法是在web.config中对此进行评论

<modules>
<remove name="FormsAuthentication"/>
</modules>

答案 3 :(得分:0)

那些仍然遇到此问题但已经尝试过上述所有方法的人我建议尝试在身份验证表单cookieless =“UseCookies”部分添加到Web.Config文件。就我而言,它运作良好。

<system.web>
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" cookieless="UseCookies" timeout="2880" />
</authentication>
...
</system.web>
相关问题