簡體   English   中英

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

[英]how to use IClaimsTransformation in core2.2 mvc project

我正在嘗試學習如何使用IClaimsTransformation來修改 windows 身份驗證中的用戶聲明。 但是當我嘗試使用它時,我收到一條錯誤消息

“InvalidOperationException:沒有指定 authenticationScheme,也沒有找到 DefaultChallengeScheme。”

我主要在mac上嘗試它,但我也在公司域的公司電腦上嘗試過。 他們都給了我同樣的錯誤。 我也是 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?}");
        });



    }

我有這個 class 用於索賠轉換

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);
    }
}

也為我的 controller 使用這個裝飾器

[Authorize(Roles = "Admin")]

首先,使用 Rider 作為 IDE 搞砸了我的調試設置,在刪除演示應用程序並將調試設置恢復為默認 IIS Express 設置后,我設法讓我的代碼正常工作。

之后,我每次嘗試調試應用程序時都會遇到 403 錯誤,在@itminus 的幫助下,我們在中間件訂單中發現了問題。 我在 UseAuthentication() 上使用 UseAuthorization() ,這是我的錯誤。 所以將 UseAuthentication() 放在 UseAuthorization() 上解決了我的第二個問題。

“InvalidOperationException:沒有指定 authenticationScheme,也沒有找到 DefaultChallengeScheme。”

幾個月前我使用 Windows 身份驗證時遇到了同樣的問題。 原來我沒有啟用Windows Authentication

請檢查屬性/調試選項卡,並確保選中Enable Windows Authentication

在此處輸入圖像描述


作為旁注,您正在修改原始principal IMO,首選返回全新的委托人:

public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
    var cp = principal.Clone();                   // create a copy
    var ci = (ClaimsIdentity)cp.Identity;
    var c = new Claim(ci.RoleClaimType, "Admin");
    ci.AddClaim(c);                               // modify the copy
    return Task.FromResult(cp);                          
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM