繁体   English   中英

调用SignInAsync后,User.Identity.IsAuthenticated返回false

[英]User.Identity.IsAuthenticated returns false after SignInAsync invoked

我有一个弹出表单,我使用jquery UI对话框加载我的登录视图。单击登录按钮时,我使用ajax post调用Account / Login操作。这是我的代码:

[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,这是为什么呢?

将不胜感激提供的任何帮助。 让我知道您是否需要其他信息来帮助回答这个问题

这是因为asp.net管道尚未对用户进行身份验证。

User.Identity.IsAuthenticated值基于HttpContext.Current。 应该发生的是SignInAsync将添加一个cookie,框架将使用该cookie对未来的请求进行身份验证。

因此,在击中该Controller动作之后。 User.Identity.IsAuthenticated在所有控制器操作中都为true。 AuthorizeAttribute基于此。

要获取用户名,我将查看UserManager.FindAsync返回的用户属性,即

var userName = user.UserName

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM