首页 > 其他分享 >openiddict add customer claims

openiddict add customer claims

时间:2023-05-15 10:14:35浏览次数:46  
标签:customer new add user claims openiddict public Identity principal

@@openid 自定义claim
@@custom claims
@@openiddict custom claims
https://stackoverflow.com/questions/38060192/asp-core-add-custom-claim-to-auth-token
#ASP Core Add Custom Claim to Auth Token
##public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
 
 
 
 
--->@@public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
https://benfoster.io/blog/customising-claims-transformation-in-aspnet-core-identity/
 
 

Customising claims transformation in ASP.NET Core Identity

I’ve been testing out the new version of ASP.NET Identity and had the need to include additional claims in the ClaimIdentity generated when a user is authenticated.

Transforming Claims Identity

ASP.NET Core supports Claims Transformation out of the box. Just create a class that implements IClaimsTransformer:

public class ClaimsTransformer : IClaimsTransformer
{
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        ((ClaimsIdentity)principal.Identity).AddClaim(new Claim("ProjectReader", "true"));
        return Task.FromResult(principal);
    }
}

To register the claims transformer, add the following inside your Configure method in Startup.cs:

app.UseClaimsTransformation(new ClaimsTransformationOptions
{
    Transformer = new ClaimsTransformer()
});

One problem with the current implementation of the claims transformation middleware is that claims transformer instances have to be created during configuration. This means no DI making it difficult to handle loading claim information from a database.

Note: I’m told this will be fixed in RC2.

Claims Identity Creation

In my application I’d extended the default ApplicationUser class with additional properties for first and last name. I wanted these properties to be included in the generated ClaimsIdentity:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

ASP.NET Core Identity has a SignInManager<TUser> responsible for signing users into your application. Internally it uses a IUserClaimsPrincipalFactory<TUser> to generate a ClaimsPrincipal from your user.

The default implementation only includes username and user identifier claims. To add additional claims we can create our own implementation of IUserClaimsPrincipalFactory<TUser> or derive from the default UserClaimsPrincipalFactory:

public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
    public AppClaimsPrincipalFactory(
        UserManager<ApplicationUser> userManager,
        RoleManager<IdentityRole> roleManager,
        IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor)
    {
    }

    public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
    {
        var principal = await base.CreateAsync(user);

        ((ClaimsIdentity)principal.Identity).AddClaims(new[] {
            new Claim(ClaimTypes.GivenName, user.FirstName),
            new Claim(ClaimTypes.Surname, user.LastName),
        });

        return principal;
    }
}

To register the custom factory we add the following to the ConfigureServices method in startup.cs after the services.AddIdentity() call:

services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
 

标签:customer,new,add,user,claims,openiddict,public,Identity,principal
From: https://www.cnblogs.com/wl-blog/p/17400993.html

相关文章

  • 百度飞桨(PaddlePaddle) - PaddleOCR 文字识别简单使用
    百度飞桨(PaddlePaddle)安装OCR文字检测(DifferentiableBinarization---DB)OCR的技术路线PaddleHub预训练模型的网络结构是DB+CRNN,可微的二值化模块(DifferentiableBinarization,简称DB)CRNN(ConvolutionalRecurrentNeuralNetwork)即卷积递归神经网络,是DCNN和RNN的......
  • Ubuntu 升级 git,如何应对sudo add-apt-repository ppa:git-core/ppa卡死的情况
    绕开代理https://blog.csdn.net/m0_68734901/article/details/128411072sudo-Eadd-apt-repositoryppa:git-core/ppasudoapt-getupdatesudoapt-getinstallgit......
  • add_custom_command用法
    环境:Linux平台:CentOSLinuxrelase7.2.1511、GCC_4.8.5-4、cmakeversion2.8.11开始:一、add_custom_command将自定义构建规则添加到生成的构建系统,有两种用法。  1.1)第一种用法:将自定义命令添加到目标,如库或可执行文件add_custom_command(TARGETtarget......
  • cli add option
    https://stackoverflow.com/questions/44284275/passing-compiler-options-in-cmake-command-lineCommandlineoptionslikeCMAKE_C_FLAGSandCMAKE_CXX_FLAGSalwaysappendandneveroverwrite.-DCMAKE_C_FLAGS-DCMAKE_CXX_FLAGS="-Wno-dev-Wl,-rpath=/home/a......
  • DedeCMS 提示信息!把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息
    DedeCMS提示信息!把数据保存到数据库附加表`dede_addonarticle`时出错,请把相关信息提交给DedeCms官方。Duplicateentry'7'forkey'PRIMARY'错误原因是Duplicateentry'7'forkey'PRIMARY'解决方案:进入后台,“系统”-“系统设置”-“SQL命令行工具”运行SQL命令行:alter......
  • docker 容器中 ip addr 出现 bash: ip: commandnot found
    一、服务器中输入命令#启动tomcat容器别名tomcat1dockerrun-d-P--nametomcat1tomcat#进行tomcat1容器dockerexec-ittomcat1/bin/bash二、输入ipaddripaddrbash:ip:commandnotfound三、解决办法安装工具iproute2#我的服务器是centos的yumi......
  • Nginx中add_header和proxy_set_header的区别
    一、proxy_set_header和add_header的区别 proxy_set_header是nginx设置请求头给上游服务器,add_header是nginx设置响应头信息给浏览器。1.1proxy_set_header 语法格式: proxy_set_headerfieldvalue; value值可以是包含文本、变量或者它们的组合。......
  • paddlepaddle docker 安装实践
    系统:linuxcentosdocker-v#Dockerversion23.0.3,build...参考链接:https://www.paddlepaddle.org.cn/documentation/docs/zh/install/docker/linux-docker.htmldockerhub:https://hub.docker.com/r/paddlepaddle/paddle/tags/拉取镜像:dockerpullpaddlepaddle/paddl......
  • Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consid
    这个警告意味着在事件监听器中,添加了一个阻止页面滚动的`mousewheel`事件,但是该事件监听器并没有标记为被动事件监听器(passiveeventlistener)。这可能会导致页面滚动变得不流畅,特别是在移动设备上。为了解决这个问题,您需要将事件监听器标记为被动事件监听器。具体实现方法如下......
  • 【MIPS汇编】ADDI,ADDIU,ADD,ADDU的区别、有符号无符号的谬误
    介绍ADDI、ADDIU:加立即数,区别在于是否检测溢出。Format:ADDIrt,rs,immediatert=rs+immediateToaddaconstanttoa32-bitinteger.Ifoverflowoccurs,thentrap.Format:ADDIUrt,rs,immediatert=rs+immediateToaddaconstanttoa32-bitinteg......