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

我想知道什么是用户身份名称,并且更改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)

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....