Request.IsAuthenticated永远是假的 [英] Request.IsAuthenticated is always false
本文介绍了Request.IsAuthenticated永远是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您能告诉我为什么 FormsAuthentication.SetAuthCookie(user.Name,FALSE);
不会导致 Request.IsAuthenticated
是真实的。
Can you tell me why FormsAuthentication.SetAuthCookie(user.Name, false);
is not causing Request.IsAuthenticated
to be true?
下面是我的代码:
[HttpPost]
public ActionResult LogIn(karcioszki.Models.UserLoginModel user)
{
if (ModelState.IsValid)
{
if (IsValid(user.Name, user.Password))
{
FormsAuthentication.SetAuthCookie(user.Name, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login or password is incorrect");
}
}
return View(user);
}
和if语句:
@if (Request.IsAuthenticated)
{
<a href="@Href("~/")" class="active">Home</a>
<a href="@Href("~/Cards")">Cards</a>
@Html.ActionLink("Log out", "Logout", "User")
@Html.Encode(User.Identity.Name)
}
另外,请告诉我,如何使它工作
Also, please tell me, how to make it work?
编辑:
我在web.config中加入身份验证(两者),但它仍然不能正常工作
I added authentication in web.config(both) but it still isn't working.
<system.web>
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<authentication mode="Windows"/>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="System.Web.Optimization" />
</namespaces>
</pages>
我应该使用Windows或其他模式?
Should I use Windows or other mode?
推荐答案
我在MVC5项目同样的问题。
解决的办法是添加以下行到模块部分中的system.webServer
I had the same problem in an MVC5 project. The solution was to add the following lines to the modules section in the system.webServer
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
这篇关于Request.IsAuthenticated永远是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文