94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
using System;
|
|
using System.Security.Claims;
|
|
using System.Security.Principal;
|
|
using System.Threading.Tasks;
|
|
using Kurs.Platform.Extensions;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Extensions.Options;
|
|
using Volo.Abp.DependencyInjection;
|
|
using Volo.Abp.Identity;
|
|
using Volo.Abp.Security.Claims;
|
|
using Volo.Abp.Uow;
|
|
using IdentityRole = Volo.Abp.Identity.IdentityRole;
|
|
using IdentityUser = Volo.Abp.Identity.IdentityUser;
|
|
|
|
namespace Kurs.Platform.Identity;
|
|
|
|
[Dependency(ReplaceServices = true)]
|
|
[ExposeServices(
|
|
typeof(AbpUserClaimsPrincipalFactory),
|
|
typeof(UserClaimsPrincipalFactory<IdentityUser, IdentityRole>))]
|
|
public class PlatformUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory, ITransientDependency
|
|
{
|
|
public PlatformUserClaimsPrincipalFactory(
|
|
UserManager<IdentityUser> userManager,
|
|
RoleManager<IdentityRole> roleManager,
|
|
IOptions<IdentityOptions> options,
|
|
ICurrentPrincipalAccessor currentPrincipalAccessor,
|
|
IAbpClaimsPrincipalFactory abpClaimsPrincipalFactory)
|
|
: base(userManager, roleManager, options, currentPrincipalAccessor, abpClaimsPrincipalFactory)
|
|
{
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public override async Task<ClaimsPrincipal> CreateAsync(IdentityUser user)
|
|
{
|
|
// We should not let abp create principal to avoid adding unnecessary claims.
|
|
// So we override this method of Volo.Abp.Identity.AbpUserClaimsPrincipalFactory,
|
|
// and use method body of Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
|
|
if (user == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(user));
|
|
}
|
|
var id = await GenerateClaimsAsync(user);
|
|
return new ClaimsPrincipal(id);
|
|
}
|
|
|
|
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(IdentityUser user)
|
|
{
|
|
var identity = await base.GenerateClaimsAsync(user);
|
|
|
|
if (user.TenantId.HasValue)
|
|
{
|
|
identity.AddIfNotContains(new Claim(AbpClaimTypes.TenantId, user.TenantId.ToString()));
|
|
}
|
|
|
|
if (!user.Name.IsNullOrWhiteSpace())
|
|
{
|
|
identity.AddIfNotContains(new Claim(AbpClaimTypes.Name, user.Name));
|
|
}
|
|
|
|
if (!user.Surname.IsNullOrWhiteSpace())
|
|
{
|
|
identity.AddIfNotContains(new Claim(AbpClaimTypes.SurName, user.Surname));
|
|
}
|
|
|
|
if (!user.PhoneNumber.IsNullOrWhiteSpace())
|
|
{
|
|
identity.AddIfNotContains(new Claim(AbpClaimTypes.PhoneNumber, user.PhoneNumber));
|
|
}
|
|
|
|
identity.AddIfNotContains(
|
|
new Claim(AbpClaimTypes.PhoneNumberVerified, user.PhoneNumberConfirmed.ToString()));
|
|
|
|
if (!user.Email.IsNullOrWhiteSpace())
|
|
{
|
|
identity.AddIfNotContains(new Claim(AbpClaimTypes.Email, user.Email));
|
|
}
|
|
|
|
identity.AddIfNotContains(new Claim(AbpClaimTypes.EmailVerified, user.EmailConfirmed.ToString()));
|
|
|
|
using (CurrentPrincipalAccessor.Change(identity))
|
|
{
|
|
var abpClaimsPrincipal = await AbpClaimsPrincipalFactory.CreateAsync();
|
|
foreach (var claim in abpClaimsPrincipal.Claims)
|
|
{
|
|
identity.AddIfNotContains(claim);
|
|
}
|
|
}
|
|
|
|
identity.AddClaim(new Claim(PlatformConsts.AbpIdentity.User.IsVerified, user.GetIsVerified().ToString()));
|
|
|
|
return identity;
|
|
}
|
|
}
|