C# Request.IsAuthenticated 总是假的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19536955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request.IsAuthenticated is always false
提问by Kmaczek
Can you tell me why FormsAuthentication.SetAuthCookie(user.Name, false);
is not causing Request.IsAuthenticated
to be true?
你能告诉我为什么FormsAuthentication.SetAuthCookie(user.Name, false);
不是Request.IsAuthenticated
真的吗?
Here is my code:
这是我的代码:
[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);
}
and if statement:
和 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?
另外,请告诉我,如何使它工作?
EDIT: I added authentication in web.config(both) but it still isn't working.
编辑:我在 web.config(both) 中添加了身份验证,但它仍然无法正常工作。
<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>
Should I use Windows or other mode?
我应该使用 Windows 还是其他模式?
采纳答案by Ger Groot
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
我在 MVC5 项目中遇到了同样的问题。解决方案是将以下几行添加到 system.webServer 中的模块部分
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
回答by AliK
You are checking if the user is authenticated. So use:
您正在检查用户是否已通过身份验证。所以使用:
if (User.Identity.IsAuthenticated)
This will fix the issue and as mentioned set the authentication element in your web.config to Forms and not Windows.
这将解决该问题,并如前所述将 web.config 中的身份验证元素设置为 Forms 而不是 Windows。
回答by Sanjay Sharma
you must set FormsAuthentication.SetAuthCookie(acct.UserName, true);
after validating user and please check you must set authentication mode="Forms"
in web.config.
您必须FormsAuthentication.SetAuthCookie(acct.UserName, true);
在验证用户后进行设置,请检查您是否必须authentication mode="Forms"
在 web.config 中设置。
回答by user2243747
I had exactly same problem in my MVC4 Web Application
. One more possible reason for this problem is below method in IdentityModels.cs
file
我的MVC4 Web Application
. 此问题的另一个可能原因是IdentityModels.cs
文件中的方法
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
Make sure DefaultAuthenticationTypes
is ApplicationCookie
确保DefaultAuthenticationTypes
是ApplicationCookie
Cheers,
干杯,
回答by Enter The Void
I was looking for about 2 hours now to solve the problem. And in case you already got setups like you are told to in several guides (as MSDN and so) and you still got the problem, the one and only thing that will solve it is to add:
我现在正在寻找大约 2 个小时来解决问题。如果您已经获得了一些指南(如 MSDN 等)中告诉您的设置,但仍然遇到问题,唯一可以解决的方法是添加:
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
</modules>
<system.webServer>
within the webconfig.
在 webconfig 中。
回答by Long Luong
Add the following code in your Web.config
在您的 Web.config 中添加以下代码
<authentication mode="Forms">
<forms loginUrl="~/_Login/Login" timeout="30" />
</authentication>
and
和
<modules>
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
</modules>
回答by Roberto Franco
In my case, it was a problem in Chrome browser. I just removed all cookies stored and works.
就我而言,这是 Chrome 浏览器的问题。我刚刚删除了所有存储的 cookie 并且可以正常工作。
回答by Chee Hou Ng
I solve it by deleting the caches and cookies in browser.
我通过删除浏览器中的缓存和cookie来解决它。
回答by Markus Knappen Johansson
In my case I had this issue which ended up in a redirect-loop where the user would be redirected to the login, successfully logged in and then back to our site but since Request.IsAuthenticated was false the user was redirected to the login agan.
在我的情况下,我遇到了这个问题,它最终出现在重定向循环中,用户将被重定向到登录名,成功登录然后返回到我们的网站,但由于 Request.IsAuthenticated 为假,用户被重定向到登录名。
In our case this was solved by using another CookieManager,
在我们的例子中,这是通过使用另一个 CookieManager 解决的,
var cookieOptions = new CookieAuthenticationOptions();
cookieOptions.CookieManager = new SystemWebCookieManager();
app.UseCookieAuthentication(cookieOptions);
Turns out that there was some kind of bug in Katana that this other CookieManager worked around, in out case this solved the issue.
事实证明,Katana 中存在一些其他 CookieManager 解决的错误,以防万一这解决了问题。
https://github.com/aspnet/AspNetKatana/wiki/System.Web-response-cookie-integration-issues
https://github.com/aspnet/AspNetKatana/wiki/System.Web-response-cookie-integration-issues