如何在core2.2 mvc项目中使用IClaimsTransformation

问题描述 投票:0回答:1

我正在尝试学习如何使用IClaimsTransformation来修改Windows身份验证中的用户声明。但是当我尝试使用它时,出现错误,提示

“ InvalidOperationException:未指定authenticationScheme,并且未找到DefaultChallengeScheme。

我主要是在Mac上尝试过,但我也在公司域中的公司PC中尝试过。他们两个都给我同样的错误。我也是IIS Express(VS和Rider的调试模式)。

在我的启动文件中

public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddAuthentication(IISDefaults.AuthenticationScheme);
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        services.AddSingleton<IClaimsTransformation, UserClaims>();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });



    }

并且我有这个类来进行权利要求转换]

public class UserClaims: IClaimsTransformation
{
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        var ci = (ClaimsIdentity) principal.Identity;
        var c = new Claim(ci.RoleClaimType, "Admin");
        ci.AddClaim(c);
        return Task.FromResult(principal);
    }
}

也将此装饰器也用作我的控制器

[Authorize(Roles = "Admin")]

我正在尝试学习如何使用IClaimsTransformation修改Windows身份验证中的用户声明。但是,当我尝试使用它时,出现错误消息“ InvalidOperationException:否...

asp.net-core .net-core asp.net-core-mvc asp.net-core-2.2
1个回答
0
投票

“ InvalidOperationException:未指定authenticationScheme,并且未找到DefaultChallengeScheme。”

© www.soinside.com 2019 - 2024. All rights reserved.