c# - 如何在登录时向 HttpContext 用户添加声明

标签 c# asp.net-mvc razor asp.net-identity claims-based-identity

这篇文章可能很长,但包含了答案所需的所有相关细节。

我一直在搜索,并发现许多其他人也在搜索向 HttpContext 用户添加声明的正确方法,以便在需要时可以使用 Razor 在 View 中检索这些声明。

例如,

在默认的 Asp.Net Core 2.0 Web 应用程序中,_LoginPartial 具有显示用户电子邮件的代码。如果我想将其更改为用户全名(这假设注册过程包括名字和姓氏条目,并对 ApplicationUser 类进行适当更改)

 // Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTime DateOfBirth { get; set; }

    public Gender Gender { get; set; }
    ...balance of code removed for brevity
}

我想为用户添加他们的全名和性别的声明,而不是默认应用程序中当前使用的 UserManager 方法。 (以及其他人)

当前默认的 wep 应用程序代码

@if (SignInManager.IsSignedIn(User))
{
    <form asp-area="" asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
            </li>
            <li>
                <button type="submit" class="btn btn-link navbar-btn navbar-link">Log out</button>
            </li>
        </ul>
    </form>
}
else
{
...code removed for brevity
}

我希望完成的事情; 替换这个,

<a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>

有了这个

<a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @((ClaimsIdentity) User.Identity).GetSpecificClaim("avatarUrl")!</a>    

注意:GetSpecificClaim 是一种检索声明的扩展方法。

我认为添加声明的最佳位置是在登录方法中。

 public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
         if (!ModelState.IsValid) return View(model);
        // Now model is valid, require the user to have a confirmed email before they can log on.
        var user = await _userManager.FindByEmailAsync(model.Email);
        if (user != null)
        {
            if (!await _userManager.IsEmailConfirmedAsync(user))
            {
                ModelState.AddModelError(string.Empty,
                    "You must have a confirmed email to log in.");
                return View(model);
            }
        }
        else
        {
            ModelState.AddModelError(string.Empty,
                "There is no registered account for the email address supplied.");
            return View(model);
        }

        var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);
        if (result.Succeeded)
        {
            _logger.LogInformation("User logged in.");

            // Add claims to signed in user 
            var userClaims = HttpContext.User.Claims.ToList();
            userClaims.Add(new Claim("fullname", user.GetFullName(user.UserName)));
            userClaims.Add(new Claim("avatarUrl", user.AvatarUrl));

           // Using ClaimsTransformer
           // Add claims here for the logged in user using AddUserInfoClaimsAsync extension method
            **var ct = new ClaimsHelpers.ClaimsTransformer();
            var identityWithInfoClaims = await ct.AddUserInfoClaimsAsync(User, user);**

            return RedirectToLocal(returnUrl);
        }
        if (result.RequiresTwoFactor)
        {
            return RedirectToAction(nameof(LoginWith2Fa), new { returnUrl, model.RememberMe });
        }
        if (result.IsLockedOut)
        {
            _logger.LogWarning("User account locked out.");
            return RedirectToAction(nameof(Lockout));
        }

        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
        return View(model);
    }

但 userClaims 变量始终为空

Breakpoint showing empty claim list

问题:

  1. 为什么刚设置claims时claims列表是空的?
  2. 是否有不同类型的身份声明?
  3. 有更好的方法吗?

更新: 我在早期的一些尝试中将 ClaimsTransformer 放在一起,我可以使用它添加声明(请参阅上面登录 Controller 代码中的粗体更改)但是我现在如何处理 ClaimsPrincipal 变量 identityWithinfoClaims?我不能将 User 设置为等于它,因为 User 是只读的,那么如何正确使用添加了声明的对象?

最佳答案

要添加或转换自定义声明,请实现并使用自定义 ClaimsAuthenticationManagerHow To: Transform Incoming Claims .

public class ClaimsTransformationModule : ClaimsAuthenticationManager {  
    public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal) {  
        if (incomingPrincipal != null && incomingPrincipal.Identity.IsAuthenticated == true) {  
           var identity = (ClaimsIdentity)incomingPrincipal.Identity;
           var user = GetUserData(identity);

           identity.AddClaim(new Claim("fullname", user.GetFullName(user.UserName)));  
           identity.AddClaim(new Claim("avatarUrl", user.AvatarUrl)); 
        }  

        return incomingPrincipal;  
    }  
} 

此处,GetUserData() 根据用户名从数据库中检索用户实体。

web.config 中注册这个转换器:

<system.identityModel>
   <identityConfiguration>
      <claimsAuthenticationManager type="MyProject.ClaimsTransformationModule , MyProject, Version=1.0.0.0, Culture=neutral" />
   </identityConfiguration>
</system.identityModel>

关于c# - 如何在登录时向 HttpContext 用户添加声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47254867/

相关文章:

c# - 绑定(bind)请求参数到具体的方法参数

c# - Visual Studio 2015- "Unable to step. The operation could not be completed. A retry should be performed"

c# - C#中使用 "var"声明变量

javascript - 如何使用javascript在@Url.Action之后执行其他函数

jquery - 如何使用 Webmatrix/razor 获取单个 json 值?

c# - ASP.Net MVC : Localized texts with new line?

jquery - Asp.net MVC with Entity,使用jquery将数据传递到List中。不起作用

c# - 无法将类型 'Task<System.Collections.Generic.IEnumerable<IClass>>' 隐式转换为 'System.Collections.Generic.IEnumerable<IClass>

c# - 使用 OrderBy<> 对项目数组进行排序

asp.net-mvc - MVC 中 HTML 帮助页面的链接