首页 > 其他分享 >How can get custom claim

How can get custom claim

时间:2023-05-29 17:36:25浏览次数:37  
标签:claim get commented custom abp How UserClaimsPrincipalFactory class

@@abp 7.0 openiddict setting token ValidateLifetime
-->https://stackoverflow.com/questions/75408673/how-can-i-change-the-openiddict-accesstoken-lifetime-in-apb-7
##--->thanks , i founded it PreConfigure<OpenIddictServerBuilder>(builder => { builder .SetAccessTokenLifetime(TimeSpan.FromDays(365)) .SetIdentityTokenLifetime(TimeSpan.FromDays(365)); });

@@abp OpenIddict OpenIddictServerBuilder
-->https://github.com/abpframework/abp/blob/dev/docs/en/Modules/OpenIddict.md
-->##https://docs.abp.io/en/abp/latest/Authorization#claims-principal-factory
-->IAbpClaimsPrincipalContributor

@@abp IAbpClaimsPrincipalContributor
https://github.com/abpframework/abp/issues/6048
#How can get custom claim · Issue #6048 · abpframework/abp
#1.Created a class inheriting from the UserClaimsPrincipalFactory class
#2.Replace the UserClaimsPrincipalFactory by pre-configuring
#

 

Closed    

How can get custom claim#6048

journey191 opened this issue on Nov 4, 2020 · 5 comments  

Comments

@journey191     journey191 commented on Nov 4, 2020

I created a class inheriting from the UserClaimsPrincipalFactory class and override the CreateAsync method,it worked.
UX3HN5@(YHGF25KERES
But I can't get my custom claim
S8OW} 1{U%XW9 VL58JS VH

 
  @maliming   Member maliming commented on Nov 4, 2020
  • Your ABP Framework version.
  • Tiered (MVC) or Identity Server Seperated (Angular): yes / no
  • Steps needed to reproduce the problem.
 
@journey191   Author journey191 commented on Nov 4, 2020

@maliming

  • Your ABP Framework version:3.1
  • Tiered (MVC) or Identity Server Seperated (Angular): no(Angular)
  • Steps needed to reproduce the problem.

1.Created a class inheriting from the UserClaimsPrincipalFactory class

public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<Volo.Abp.Identity.IdentityUser, Volo.Abp.Identity.IdentityRole>, ITransientDependency
    {
        private readonly IRepository<UserPosition> _userPositionRepository;
        private readonly IHttpContextAccessor _httpContext;

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

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

            if (user.TenantId.HasValue)
            {
                principal.Identities
                    .First()
                    .AddClaim(new Claim(AbpClaimTypes.TenantId, user.TenantId.ToString()));
            }

            var userPositionList = await _userPositionRepository.Where(_ => _.UserId == user.Id).ToListAsync();
            string positionIds = "";
            foreach (var item in userPositionList)
            {
                positionIds += positionIds == "" ? item.PositionId.ToString() : "," + item.PositionId.ToString();
            }
            principal.Identities
                    .First()
                    .AddClaim(new Claim("PositionIds", positionIds));

            return principal;
        }
    }

2.Replace the UserClaimsPrincipalFactory by pre-configuring

public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            OntallCoreDomainObjectExtensions.Configure();

            PreConfigure<IdentityBuilder>(builder =>
            {
                builder.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>();
            });
        }

I can't get my custom claim

var positionIds = CurrentUser.FindClaimValue("PositionIds");
// var claims = CurrentUser.GetAllClaims();

But Now I resolve the problem by adding ApiResource in IdentityServerDataSeedContributor.cs

private async Task CreateApiResourcesAsync()
        {
            var commonApiUserClaims = new[]
            {
                "email",
                "email_verified",
                "name",
                "phone_number",
                "phone_number_verified",
                "role",
                "PositionIds"
            };

            await CreateApiResourceAsync("OntallCore", commonApiUserClaims);
        }

相关文章

  • show parameter sga;
    安装Oracle时,为了均衡电脑性能和数据库性能,Oracle一个实例默认内存占用大小为物理内存的1/8。如环境不需要分配那么大的内存来支撑Oracle,可通过修改sga_max_size的值来减少系统中内存占用过大问题。步骤如下:1.cmdsqlplussystem账户登录2.showparametersga;--显示内存分......
  • ctfshow刷题笔记-misc入门
    ctfshow-misc入门图片篇(文件结构)misc241.在010Editor中打开文件,根据鼠标自动提示找到图片宽高对应的地方biWidth指定图象的宽度,单位是象素。biHeight指定图象的高度,单位是象素。2.修改图片高度为250px并另存3.打开后得到flagmisc251.从网上找到的脚本(将脚本和图片......
  • How to boot the Raspberry Pi system from a USB Mass Storage Device All In One
    HowtoboottheRaspberryPisystemfromaUSBMassStorageDeviceAllInOne如何从USB启动树莓派引导系统/如何从USB大容量存储设备启动RaspberryPi系统https://www.raspberrypi.com/news/pi-3-booting-part-i-usb-mass-storage-boot/officaildocsThispag......
  • Webpack and Babel — What are they, and how to use them with React
    摘抄自:https://medium.com/@agzuniverse/webpack-and-babel-what-are-they-and-how-to-use-them-with-react-5807afc82ca8WebpackandBabel—Toolswecan’tcodewithoutWe’llbeconfiguringbothoftheseforourReactproject,sofirsthere’saquickexplanation......
  • CTFSHOW
    RCE挑战1<?phperror_reporting(0);highlight_file(__FILE__);$code=$_POST['code'];$code=str_replace("(","括号",$code);$code=str_replace(".","点",$code);eval($code);?>把"."和&quo......
  • 第一个react.js程序:create and show comment
    importReact,{Component}from"react";import{render}from"react-dom";importPropTypesfrom"prop-types";constnode=document.getElementById("root");constdata={post:{id:123,content:......
  • How to Control an External USB Web Camera Using a Raspberry Pi All In One
    HowtoControlanExternalUSBWebCameraUsingaRaspberryPiAllInOne如何使用树莓派控制外接USB网络摄像头USB网络摄像头罗技C270i高清网络摄像头(视频120万像素,照片300万像素)1280x720=>720P1280*720//921600不足100万,90万✅PCh......
  • how to strip debug info
    compile:c++-gtmp.cc-ostarrocks_besplitdebuginfoobjcopy--only-keep-debugstarrocks_bestarrocks_be.debugstrip--strip-debugstarrocks_belinkdebuginfowithstripedbinaryobjcopy--add-gnu-debuglink=starrocks_be.debugstarrocks_beoptional......
  • How to initialize a static const map in c++?
    #include<map>usingnamespacestd;structA{staticmap<int,int>create_map(){map<int,int>m;m[1]=2;m[3]=4;m[5]=6;returnm;}staticconstma......
  • How to fix CMake error Could not find a package configuration file provided by
    CMakeErrorat/usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake:117(find_package):Couldnotfindapackageconfigurationfileprovidedby"boost_filesystem"(requestedversion1.71.0)withanyofthefollowingnames:boos......