ASP.NET Core 2.0中缺少声明转换支持 [英] Claims transformation support missing in ASP.NET Core 2.0

查看:98
本文介绍了ASP.NET Core 2.0中缺少声明转换支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新的asp.net core 2.0 api应用程序中使用JWT Bearer auth,并想对当前身份添加一些额外的声明.此额外信息位于另一个需要查询的api中.我的理解是主张转换将是执行此操作的适当位置.在.net core 1.1中,Microsoft.AspNetCore.Authentication nuget包中具有IClaimsTransformer接口,但是我无法在.net core 2.0应用程序中安装此接口.在asp.net core 2.0中是否有其他方法可以转换声明,这对我的用例来说是正确的方法吗?

I am using JWT Bearer auth in my new asp.net core 2.0 api app and want to add some extra claims to the current identity. This extra info is located in another api which need to be queried. My understanding is that claims transformation would be the proper place to do this. In .net core 1.1 you have the IClaimsTransformer interface in Microsoft.AspNetCore.Authentication nuget package, but I cannot install this one in my .net core 2.0 app. Is there a alternative way to transform claims in asp.net core 2.0 and is this the correct approach for my use case?

推荐答案

IClaimsTransformer在ASP.NET Core 2.0中已重命名为IClaimsTransformation.

IClaimsTransformer has been renamed to IClaimsTransformation in ASP.NET Core 2.0.

索赔转换更简单,新的IClaimsTransformation服务 单个方法:Task TransformAsync(ClaimsPrincipal 委托人),我们会在任何成功的AuthenticateAsync调用中调用它.

Claims Transformation Simpler, new IClaimsTransformation service with a single method: Task TransformAsync(ClaimsPrincipal principal) We call this on any successful AuthenticateAsync call.

services.AddSingleton<IClaimsTransformation, ClaimsTransformer>();

private class ClaimsTransformer : IClaimsTransformation {
    // Can consume services from DI as needed, including scoped DbContexts
    public ClaimsTransformer(IHttpContextAccessor httpAccessor) { }
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal p) {
        p.AddIdentity(new ClaimsIdentity());
        return Task.FromResult(p);
    }
}

请参见 https://github.com/aspnet/Security/issues/1310

这篇关于ASP.NET Core 2.0中缺少声明转换支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆