调用SignInAsync后,User.Identity.IsAuthenticated返回false [英] User.Identity.IsAuthenticated returns false after SignInAsync invoked
问题描述
我有一个弹出表单,我使用jquery UI对话框加载我的登录视图.单击登录"按钮时,我使用ajax post调用帐户/登录"操作.这是我的代码:
I have a pop up form in that form I load my Login view using jquery UI dialog .When the Login button is clicked I invoke the Account/Login action using ajax post .Here is my code :
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
//IsAuthenticated is returns false
bool isAuthenticated= User.Identity.IsAuthenticated;
return PartialView("_LoginPartial");
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
在调用SignInAsync方法之后,User.Identity.IsAuthenticated返回false为何会发生这种情况?
after the SignInAsync method is invoked ,the User.Identity.IsAuthenticated is returns false why thats happening?
将感谢您提供的任何帮助.让我知道您是否需要其他信息来帮助回答这个问题
Would appreciate any help offered. Let me know if you need any other information to help answer this question
推荐答案
这是因为asp.net管道尚未对用户进行身份验证.
This because the asp.net pipeline has not authenticated the user.
User.Identity.IsAuthenticated
值基于HttpContext.Current.应该发生的是 SignInAsync
将添加一个cookie,框架将使用该cookie来认证将来的请求.
The User.Identity.IsAuthenticated
value is based on HttpContext.Current. What should happen is SignInAsync
will add a cookie that the framework will use to authenticate future requests.
因此,在击中该Controller动作之后. User.Identity.IsAuthenticated
在所有控制器操作中均为true. AuthorizeAttribute
基于此.
So after this Controller action has been hit. User.Identity.IsAuthenticated
will be true in all controller actions. The AuthorizeAttribute
is based on this.
要获取用户名,我将查看 UserManager.FindAsync
返回的用户属性
To get the username i would look at the user property that UserManager.FindAsync
has returned i.e.
var userName = user.UserName
这篇关于调用SignInAsync后,User.Identity.IsAuthenticated返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!