加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 什么设置的User.Identity.Name和User.Identity.IsAut

发布时间:2020-12-16 06:39:14 所属栏目:asp.Net 来源:网络整理
导读:我想知道什么是用户身份名称,并且更改isAuthenticated为true. 为什么在SignInManager.PasswordSignInAsync返回Success后,User.Identity.Name为空字符串且User.Identity.IsAuthenticated为false. // POST: /Account/Login[HttpPost][AllowAnonymous][Validate
我想知道什么是用户身份名称,并且更改isAuthenticated为true.

为什么在SignInManager.PasswordSignInAsync返回Success后,User.Identity.Name为空字符串且User.Identity.IsAuthenticated为false.

// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model,string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var userIdentityNameTest = User.Identity.Name; // Empty string

    var result = await SignInManager.PasswordSignInAsync(
                                             model.Email,model.Password,model.RememberMe,shouldLockout: false);
    // result is "Success"

    userIdentityNameTest = User.Identity.Name;
    // userIdentityNameTest is still an empty string?
    // User.Identity.IsAuthenticated is still false?

    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode",new { ReturnUrl = returnUrl,RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("","Invalid login attempt.");
            return View(model);
    }
}

解决方法

似乎SignInManager.PasswordSignInAsync仅验证输入的数据,如果您没有使用TwoFactorAuthentication,则运行AuthenticationManager.SignIn.在这种情况下,AuthenticationManager.SignIn仅将身份验证cookie设置为响应.

因此,User.Identity可在随后的应用程序请求中使用.要按名称获取ApplicationUser,您可以使用ApplicationUserManager,如下所示:

UserManager.FindByNameAsync(model.Name)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读