【发布时间】:2022-01-17 22:23:36
【问题描述】:
所以,我遵循了 Microsoft Docs 和 Medium article 中的指导方针,它们建议将 OIDC 身份验证添加到 Blazor WASM 应用程序应该很容易。
我已经有一个可以工作的 OIDC-Server (Keycloak),我已经成功地将它用于多个 Blazor Server 应用程序。
然后我在appsettings.json 中定义了所需的属性,如下所示:
{
"OIDC": {
"Authority": "https://mykeycloakserver/auth/realms/master/",
"ClientId": "MyClientId",
"ClientSecret": "FooBarBaz-813361955dc3",
"DefaultScopes": [
"openid",
"profile",
"roles",
"offline_access"
],
"PostLogoutRedirectUri": "/",
"ResponseType": "code"
}
}
并将其添加到program.cs
builder.Services.AddOidcAuthentication(options =>
{
builder.Configuration.Bind("OIDC", options.ProviderOptions);
});
应用程序(和服务器 API)启动,我单击登录并收到以下消息:
然后,我是通过appsetting.json 定义RedirectUri 还是像这样在program.cs 中明确定义都没有关系:
options.ProviderOptions.RedirectUri = "https://localhost:7066/authentication/login-callback";
它总是告诉我它是未定义的。在配置绑定后设置断点告诉我我的设置确实被拾取了!
完整的program.cs:
using MyWASM.Client;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddHttpClient("AnliegenWASM.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
// Supply HttpClient instances that include access tokens when making requests to the server project
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("MyWASM.ServerAPI"));
builder.Services.AddOidcAuthentication(options =>
{
builder.Configuration.Bind("OIDC", options.ProviderOptions);
//options.ProviderOptions.RedirectUri = "https://localhost:7066/authentication/login-callback";
});
await builder.Build().RunAsync();
那么,我错过了什么?
【问题讨论】:
-
谁在抛出无法阅读的 RedirectUrl?您的 Blazor 应用程序或 KeyCloack? keycloak 中的 redirectUri 是如何配置的?此外,通常在代码中而不是 Json 中配置 AddOpenIDConnect 会更容易,然后您可以从等式中删除 JSON 输入错误。
-
我很确定 OIDC 服务器必须配置客户端和返回地址,也许这就是错误所在。
-
不,这不是错误所在。重定向 Uri 在 Keycloak 服务器上定义良好。这是一条客户端错误消息,如果是服务器错误,服务器会自己告诉我。
标签: c# blazor openid-connect webassembly