【问题标题】:Hosted Blazor WASM with identity call from second domain return 401使用来自第二个域的身份调用的托管 Blazor WASM 返回 401
【发布时间】:2021-01-22 23:31:18
【问题描述】:

我有一个带有身份服务器 4 的 asp.net 核心托管 blazor wasm 应用程序用于身份验证。我使用默认模板来设置所有内容,并且只进行了微小的更改。这是我的相关启动代码:

        services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, SupportToolContext>(options =>
                {
                    options.IdentityResources["openid"].UserClaims.Add("name");
                    options.ApiResources.Single().UserClaims.Add("name");
                    options.IdentityResources["openid"].UserClaims.Add("role");
                    options.ApiResources.Single().UserClaims.Add("role");
                });

        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("role");

        services.AddAuthorization(options =>
        {
            options.AddPolicy(AdministratorPolicy.AdministratorPolicyName, AdministratorPolicy.IsAdminPolicy());
            options.AddPolicy(EmployeePolicy.EmployeePolicyName, EmployeePolicy.IsEmployeePolicy());
        });

        services.AddAuthentication()
                .AddIdentityServerJwt();

        services.Configure<IdentityOptions>(options =>
        {
            // Password settings
            options.Password.RequireDigit = false;
            options.Password.RequiredLength = MIN_PASSWORD_LENGTH;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;

            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(LOCKOUT_DURATION);
            options.Lockout.MaxFailedAccessAttempts = MAX_TRIES_BEFORE_LOCKOUT;
            options.Lockout.AllowedForNewUsers = true;

            // Email Settings
            options.User.RequireUniqueEmail = true;
            options.SignIn.RequireConfirmedEmail = true;
        });

        // Configure LifeSpan of Identity email tokens
        services.Configure<DataProtectionTokenProviderOptions>(options =>
        {
            options.TokenLifespan = TimeSpan.FromDays(IDENTITY_TOKEN_DURATION);
        });

        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.HttpOnly = false;
            options.Events = new CookieAuthenticationEvents
            {
                OnRedirectToLogin = context =>
                {
                    context.Response.StatusCode = UNAUTHORIZED_STATUS_CODE;
                    return Task.CompletedTask;
                }
            };
        });

我在本地使用密钥的开发设置,在 Azure 上我有一个用于生产的自签名证书。只要我通过我的 .ch 域登录,它就可以工作。当我通过我的 .com 域访问时,我可以按预期登录,但在 API 调用中我收到此错误:

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Response status code does not indicate success: 401 (Unauthorized).
System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
  at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () <0x3639fa8 + 0x00052> in <filename unknown>:0 
  at System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsyncCore[T] (System.Threading.Tasks.Task`1[TResult] taskResponse, System.Text.Json.JsonSerializerOptions options, System.Threading.CancellationToken cancellationToken) <0x3912f98 + 0x0012a> in <filename unknown>:0 
  at ApplySupportTool.Client.Services.Implementations.UserProfileApi.GetUserProfileAsync () <0x3912300 + 0x000f8> in <filename unknown>:0 
  at ApplySupportTool.Client.Components.ProfilePartial.OnInitializedAsync () <0x390f088 + 0x000d4> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2a529e8 + 0x00154> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x33c4330 + 0x000ca> in <filename unknown>:0 

当我直接检查请求时,它指出:

www-authenticate: Bearer error="invalid_token", error_description="The issuer '[Domain].com' is invalid"

似乎默认情况下,发行者会自动设置为某个值。有没有办法可以手动将其设置为单个值或添加多个受支持的发行者?

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    您可以在 IdentityServer 选项中设置 IssuerUri,详情请参阅此页面:

    https://identityserver4.readthedocs.io/en/latest/reference/options.html

    您应该在系统中只有一个发行人,否则会令人困惑......

    在天蓝色的一个问题是,您可能有一个公共客户端可以看到的公共域,但在内部,服务本身会看到一个内部域名称。如果您在应用程序之外终止 HTTPS/TLS,则尤其常见。我认为可以将 IssuerUri 设置为客户用来与之通信的公共域名。

    【讨论】:

    • 嗯,我明白了。一个应该如何支持多个端点?在您的链接中,他们声明不建议设置此属性并让客户端根据主机名确定颁发者。但似乎我的服务器仅将三分之一的可能颁发者识别为有效(天蓝色的默认 URL 也不被识别为有效..)
    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 2021-04-11
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2021-05-29
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多