UserManager.GetUserAsync 返回 null

问题描述 投票:0回答:1

我有两个项目。项目1是一个VB项目,项目2是一个.NET Core MVC项目。

项目 1 向项目 2 发送数据。根据收到的数据,它在数据库中创建一个新的身份用户并让该用户登录。这一切都按预期进行。

    public async Task<IActionResult> CreateExternalUser(string externalJson)
{
    var externalTokenDetails = JsonConvert.DeserializeObject<ExternalTokenDetails>(externalJson);

    var company = _admin.GetCompanyByClientCode(externalTokenDetails.ClientCode);
    if (company != null)
    {
        var existingUser = await _userManager.FindByEmailAsync(externalTokenDetails.Email);
        if (existingUser != null)
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, existingUser.UserName),
                new Claim(ClaimTypes.Email, existingUser.Email),
                new Claim("FullName", $"{externalTokenDetails.Name} {externalTokenDetails.Surname}"),
                new Claim("ClientCode", externalTokenDetails.ClientCode),
                new Claim("ClientName", externalTokenDetails.ClientName),
                new Claim(ClaimTypes.Role, externalTokenDetails.RoleName),
            };

            var userIdentity = new ClaimsIdentity(claims, IdentityConstants.ApplicationScheme);
            var userPrincipal = new ClaimsPrincipal(userIdentity);

            await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, userPrincipal);

            return RedirectToAction("Index", "Home");
        }
        else
        {
            var user = new ApplicationUser
            {
                UserName = externalTokenDetails.Email,
                Email = externalTokenDetails.Email,
                CompanyId = company.Id,
                Company = company,
            };

            var result = await _userManager.CreateAsync(user); // Persist the user

            if (result.Succeeded)
            {
                // User created successfully, sign them in
                var claims = new List<Claim>
                {
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(ClaimTypes.Email, user.Email),
                    new Claim("FullName", $"{externalTokenDetails.Name} {externalTokenDetails.Surname}"),
                    new Claim("ClientCode", externalTokenDetails.ClientCode),
                    new Claim("ClientName", externalTokenDetails.ClientName),
                    new Claim(ClaimTypes.Role, externalTokenDetails.RoleName),
                }; 
                
                var userIdentity = new ClaimsIdentity(claims, IdentityConstants.ApplicationScheme);
                var userPrincipal = new ClaimsPrincipal(userIdentity);

                await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, userPrincipal);

                return RedirectToAction("Index", "Home");
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }

}

我遇到的问题是在我的 HomeController,Index 方法中。

    System.Security.Claims.ClaimsPrincipal currentUser = this.User;
    bool isAdmin = currentUser.IsInRole("Admin");

           
    var id = _userManager.GetUserId(User); // Get user id:

    var user = await _userManager.GetUserAsync(User);

在这种情况下,重定向到此操作后,

id
user
为空

为什么会这样?我还有哪些其他选择来获取这些值?

.net asp.net-core asp.net-identity
1个回答
0
投票

我在这个问题的帮助下解决了它 - JWT Authentication - UserManager.GetUserAsync returns null

默认情况下,getUserId 和 GetUserAsync 查看 NameIdentifier 声明。我将其添加到引用 userId 的声明中,它解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.