新文章 网摘 文章 随笔 日记

abp vnext自定义claim


using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
using Volo.Abp.Security.Claims;
using Volo.Abp.Uow;
using DependencyAttribute = Volo.Abp.DependencyInjection.DependencyAttribute;

namespace Hang.Blog.Web
{
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(AbpUserClaimsPrincipalFactory))]
public class MyUserClaimsPrincipalFactory :
AbpUserClaimsPrincipalFactory, ITransientDependency
{
private readonly IHttpContextAccessor _httpContext;

public MyUserClaimsPrincipalFactory(
UserManager<Volo.Abp.Identity.IdentityUser> userManager,
RoleManager<Volo.Abp.Identity.IdentityRole> roleManager,
IOptions<IdentityOptions> options,
IHttpContextAccessor httpContext)
: base(
userManager,
roleManager,
options)
{
_httpContext = httpContext;
}

[UnitOfWork]
public override async Task<ClaimsPrincipal> CreateAsync(Volo.Abp.Identity.IdentityUser user)
{
var principal = await base.CreateAsync(user);


principal.Identities
.First().AddClaim(new Claim("Test", "Test"));
return principal;
}
}
}
private async Task CreateApiResourcesAsync()
{
var commonApiUserClaims = new[]
{
"email",
"email_verified",
"name",
"phone_number",
"phone_number_verified",
"role",
"Test",

};

await CreateApiResourceAsync("Blog", commonApiUserClaims);
}
 
————————————————
版权声明:本文为CSDN博主「hangyejiadao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hangyejiadao/article/details/113881113

posted @ 2021-04-19 08:43  岭南春  阅读(318)  评论(0)    收藏  举报