简体   繁体   English

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

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

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 : 我有一个弹出表单,我使用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);
}

after the SignInAsync method is invoked ,the User.Identity.IsAuthenticated is returns false why thats happening? 调用SignInAsync方法后,User.Identity.IsAuthenticated返回false,这是为什么呢?

Would appreciate any help offered. 将不胜感激提供的任何帮助。 Let me know if you need any other information to help answer this question 让我知道您是否需要其他信息来帮助回答这个问题

This because the asp.net pipeline has not authenticated the user. 这是因为asp.net管道尚未对用户进行身份验证。

The User.Identity.IsAuthenticated value is based on HttpContext.Current. User.Identity.IsAuthenticated值基于HttpContext.Current。 What should happen is SignInAsync will add a cookie that the framework will use to authenticate future requests. 应该发生的是SignInAsync将添加一个cookie,框架将使用该cookie对未来的请求进行身份验证。

So after this Controller action has been hit. 因此,在击中该Controller动作之后。 User.Identity.IsAuthenticated will be true in all controller actions. User.Identity.IsAuthenticated在所有控制器操作中都为true。 The AuthorizeAttribute is based on this. AuthorizeAttribute基于此。

To get the username i would look at the user property that UserManager.FindAsync has returned ie 要获取用户名,我将查看UserManager.FindAsync返回的用户属性,即

var userName = user.UserName

暂无
暂无

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

相关问题 User.Identity.IsAuthenticated始终返回false - User.Identity.IsAuthenticated always returns false 登录成功后 User.Identity.IsAuthenticated 为 false - User.Identity.IsAuthenticated is false after successful login PasswordSignInAsync() 成功后 User.Identity.IsAuthenticated 为 false - User.Identity.IsAuthenticated is false after PasswordSignInAsync() succeeded User.Identity.IsAuthenticated始终为false - User.Identity.IsAuthenticated is always false 为什么User.Identity.IsAuthenticated false? - Why User.Identity.IsAuthenticated false? 身份登录/ PasswordSignIn返回成功,但@ User.Identity.IsAuthenticated返回false - Identity signIn / PasswordSignIn returns success, but @User.Identity.IsAuthenticated returns false 即使在使用asp.net core 2.2中的PasswordSignInAsync成功登录后,User.Identity.IsAuthenticated也始终返回false - User.Identity.IsAuthenticated always returns false even after successfully logged in using PasswordSignInAsync in asp.net core 2.2 HttpContext.SignInAsync() 无法设置 cookie 并将 User.Identity.IsAuthenticated 返回为 true - HttpContext.SignInAsync() fails to set cookie and return User.Identity.IsAuthenticated as true SignInManager.PasswordSignInAsync() 成功,但 User.Identity.IsAuthenticated 为 false - SignInManager.PasswordSignInAsync() succeeds, but User.Identity.IsAuthenticated is false User.Identity.IsAuthenticated 在使用 ITFoxtec 的 SAML 2.0 中始终为假 - User.Identity.IsAuthenticated Always false in SAML 2.0 using ITFoxtec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM