【问题标题】:(Request.IsAuthenticated) is false after using FormsAuthentication.SetAuthCookie(username,false)(Request.IsAuthenticated) 使用 FormsAuthentication.SetAuthCookie(username,false) 后为假
【发布时间】:2013-10-17 07:57:16
【问题描述】:

我正在使用 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 身份验证。

【问题讨论】:

  • 答案很好,但我认为这个问题与 MVC 5 无关,因为我在 MVC 4 中遇到了同样的问题。

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


【解决方案1】:

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

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

【讨论】:

  • 我遇到了同样的问题!将您的答案添加到 web.config 工作正常! request.IsAuthenticated 返回 true
【解决方案2】:

如果你不想对抗MVC5新的认证模式(OWIN)你可以在这个链接中找到你的答案

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      那些仍然有这个问题并尝试了以上所有方法的人,我建议尝试在身份验证表单 cookieless="UseCookies" 部分中添加到 Web.Config 文件。就我而言,它运行良好。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-23
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 2016-12-13
        • 2014-01-10
        • 2020-10-10
        相关资源
        最近更新 更多