【发布时间】: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