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

chr*_*ris 4 forms-authentication asp.net-mvc-5

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

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

System.Web.Security.FormsAuthentication.SetAuthCookie(username, false);
Run Code Online (Sandbox Code Playgroud)

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

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

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

chr*_*ris 16

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

<system.web>
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
...
</system.web>
Run Code Online (Sandbox Code Playgroud)