din*_*tom 6 c# asp.net-mvc claims-based-identity razor asp.net-identity
这篇文章可能很长,但将包含答案所需的所有相关细节。
我一直在搜索,并发现许多其他人也有向 HttpContext 用户添加声明的正确方法,以便可以在需要时使用 Razor 在视图中检索这些声明。
例如,
在默认的 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
}
Run Code Online (Sandbox Code Playgroud)
我想为用户添加他们的全名和性别的声明,而不是默认应用程序中当前使用的 UserManager 方法。(还有其他人在路上)
当前默认的 web 应用程序代码
@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
}
Run Code Online (Sandbox Code Playgroud)
我希望完成的事情;更换这个,
<a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
Run Code Online (Sandbox Code Playgroud)
有了这个
<a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @((ClaimsIdentity) User.Identity).GetSpecificClaim("avatarUrl")!</a>
Run Code Online (Sandbox Code Playgroud)
注意: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);
}
Run Code Online (Sandbox Code Playgroud)
但是 userClaims 变量始终为空
问题:
更新: 我在之前的一些尝试中将 ClaimsTransformer 放在一起,我可以使用它添加声明(请参阅上面登录控制器代码中以粗体显示的更改)但是我现在如何处理 ClaimsPrincipal 变量 identityWithinfoClaims?我无法将 User 设置为等于它,因为 User 是只读的,那么如何正确使用添加了声明的对象?
要添加或转换自定义声明,请实现并使用自定义ClaimsAuthenticationManager. 如何:转变收到的索赔。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
此处,GetUserData()根据给定的用户名从数据库检索用户实体。
在以下位置注册此变压器web.config:
<system.identityModel>
<identityConfiguration>
<claimsAuthenticationManager type="MyProject.ClaimsTransformationModule , MyProject, Version=1.0.0.0, Culture=neutral" />
</identityConfiguration>
</system.identityModel>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11300 次 |
| 最近记录: |