IClaims变换未触发 [英] IClaimsTransformation not firing

查看:0
本文介绍了IClaims变换未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试实现在ASP.NET core 3.1 Web应用程序中找到的IClaimsTransformation

public class ClaimsTransformer : IClaimsTransformation
{
    public ClaimsTransformer(IHttpContextAccessor httpAccessor) { }

    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal p)
    {
        p.AddIdentity(new ClaimsIdentity());
        return Task.FromResult(p);
    }
}

我是这样添加的:

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyConString")));

        services.AddAuthentication(IISDefaults.AuthenticationScheme);

        services.AddSingleton<IClaimsTransformation, ClaimsTransformer>();

        services.AddIdentity<IdentityUser, IdentityRole>()
            .AddRoleManager<RoleManager<IdentityRole>>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        ...

当构造函数在启动期间被击中时,TransformAsync()方法在访问控制器时从不触发。我遗漏了什么?有几个添加IClaimsTransformation的实现,但除了类和通过单例/瞬时添加

之外,没有一个实现有更多的信息

虽然我应该补充一下,但我已经看到了app.UseClaimsTransformation()的各种示例,但3.1中没有提供

推荐答案

您似乎遇到了相同的问题,已解决here

AddAuthentication设置DefaultSolutions(Windows),但AddIdentity设置为Auth中间件优先的DefaultAuthenticateSolutions(Identity.Application)。您没有使用身份Cookie登录,因此声明转换不会运行。注释掉AddIdentity会使您返回到Windows身份验证,并允许运行转换。在AddIdentity之后将DefaultAuthenticateSolutions设置为IISDefaults.AuthenticationSolutions也可以修复它。

这篇关于IClaims变换未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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