Browse Source

Call `AbpClaimsPrincipalFactory` in` AbpUserClaimsFactory`.

Resolve #14343
pull/14344/head
maliming 4 years ago
parent
commit
cbe50a2f92
  1. 47
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpUserClaimsFactory.cs

47
modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpUserClaimsFactory.cs

@ -7,6 +7,7 @@ using IdentityModel;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.JsonWebTokens;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Claims;
using IdentityUser = Volo.Abp.Identity.IdentityUser; using IdentityUser = Volo.Abp.Identity.IdentityUser;
namespace Volo.Abp.IdentityServer.AspNetIdentity; namespace Volo.Abp.IdentityServer.AspNetIdentity;
@ -14,30 +15,37 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity;
public class AbpUserClaimsFactory<TUser> : IUserClaimsPrincipalFactory<TUser> public class AbpUserClaimsFactory<TUser> : IUserClaimsPrincipalFactory<TUser>
where TUser : class where TUser : class
{ {
private readonly IObjectAccessor<IUserClaimsPrincipalFactory<TUser>> _inner; protected IObjectAccessor<IUserClaimsPrincipalFactory<TUser>> Inner { get; }
private readonly UserManager<TUser> _userManager; protected UserManager<TUser> UserManager { get; }
protected ICurrentPrincipalAccessor CurrentPrincipalAccessor { get; }
protected IAbpClaimsPrincipalFactory AbpClaimsPrincipalFactory { get; }
public AbpUserClaimsFactory(IObjectAccessor<IUserClaimsPrincipalFactory<TUser>> inner, public AbpUserClaimsFactory(
UserManager<TUser> userManager) IObjectAccessor<IUserClaimsPrincipalFactory<TUser>> inner,
UserManager<TUser> userManager,
ICurrentPrincipalAccessor currentPrincipalAccessor,
IAbpClaimsPrincipalFactory abpClaimsPrincipalFactory)
{ {
_inner = inner; Inner = inner;
_userManager = userManager; UserManager = userManager;
CurrentPrincipalAccessor = currentPrincipalAccessor;
AbpClaimsPrincipalFactory = abpClaimsPrincipalFactory;
} }
public async Task<ClaimsPrincipal> CreateAsync(TUser user) public virtual async Task<ClaimsPrincipal> CreateAsync(TUser user)
{ {
var principal = await _inner.Value.CreateAsync(user); var principal = await Inner.Value.CreateAsync(user);
var identity = principal.Identities.First(); var identity = principal.Identities.First();
if (!identity.HasClaim(x => x.Type == JwtClaimTypes.Subject)) if (!identity.HasClaim(x => x.Type == JwtClaimTypes.Subject))
{ {
var sub = await _userManager.GetUserIdAsync(user); var sub = await UserManager.GetUserIdAsync(user);
identity.AddIfNotContains(new Claim(JwtClaimTypes.Subject, sub)); identity.AddIfNotContains(new Claim(JwtClaimTypes.Subject, sub));
} }
var username = await _userManager.GetUserNameAsync(user); var username = await UserManager.GetUserNameAsync(user);
var usernameClaim = identity.FindFirst(claim => var usernameClaim = identity.FindFirst(claim =>
claim.Type == _userManager.Options.ClaimsIdentity.UserNameClaimType && claim.Value == username); claim.Type == UserManager.Options.ClaimsIdentity.UserNameClaimType && claim.Value == username);
if (usernameClaim != null) if (usernameClaim != null)
{ {
identity.RemoveClaim(usernameClaim); identity.RemoveClaim(usernameClaim);
@ -53,25 +61,25 @@ public class AbpUserClaimsFactory<TUser> : IUserClaimsPrincipalFactory<TUser>
identity.AddIfNotContains(new Claim(JwtClaimTypes.Name, username)); identity.AddIfNotContains(new Claim(JwtClaimTypes.Name, username));
} }
if (_userManager.SupportsUserEmail) if (UserManager.SupportsUserEmail)
{ {
var email = await _userManager.GetEmailAsync(user); var email = await UserManager.GetEmailAsync(user);
if (!string.IsNullOrWhiteSpace(email)) if (!string.IsNullOrWhiteSpace(email))
{ {
identity.AddIfNotContains(new Claim(JwtClaimTypes.Email, email)); identity.AddIfNotContains(new Claim(JwtClaimTypes.Email, email));
identity.AddIfNotContains(new Claim(JwtClaimTypes.EmailVerified, identity.AddIfNotContains(new Claim(JwtClaimTypes.EmailVerified,
await _userManager.IsEmailConfirmedAsync(user) ? "true" : "false", ClaimValueTypes.Boolean)); await UserManager.IsEmailConfirmedAsync(user) ? "true" : "false", ClaimValueTypes.Boolean));
} }
} }
if (_userManager.SupportsUserPhoneNumber) if (UserManager.SupportsUserPhoneNumber)
{ {
var phoneNumber = await _userManager.GetPhoneNumberAsync(user); var phoneNumber = await UserManager.GetPhoneNumberAsync(user);
if (!string.IsNullOrWhiteSpace(phoneNumber)) if (!string.IsNullOrWhiteSpace(phoneNumber))
{ {
identity.AddIfNotContains(new Claim(JwtClaimTypes.PhoneNumber, phoneNumber)); identity.AddIfNotContains(new Claim(JwtClaimTypes.PhoneNumber, phoneNumber));
identity.AddIfNotContains(new Claim(JwtClaimTypes.PhoneNumberVerified, identity.AddIfNotContains(new Claim(JwtClaimTypes.PhoneNumberVerified,
await _userManager.IsPhoneNumberConfirmedAsync(user) ? "true" : "false", await UserManager.IsPhoneNumberConfirmedAsync(user) ? "true" : "false",
ClaimValueTypes.Boolean)); ClaimValueTypes.Boolean));
} }
} }
@ -89,6 +97,11 @@ public class AbpUserClaimsFactory<TUser> : IUserClaimsPrincipalFactory<TUser>
} }
} }
using (CurrentPrincipalAccessor.Change(identity))
{
await AbpClaimsPrincipalFactory.CreateAsync(principal);
}
return principal; return principal;
} }
} }

Loading…
Cancel
Save