【问题标题】:Blazor WASM OIDC stating that redirect uri is undefined whatever I doBlazor WASM OIDC 声明无论我做什么重定向 uri 都是未定义的
【发布时间】:2022-01-17 22:23:36
【问题描述】:

所以,我遵循了 Microsoft DocsMedium 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


【解决方案1】:

我也遇到了这个问题。仔细检查添加了 JS AuthenticationService 的 index.html 页面。

我让它引用了 MSAL 身份验证服务:

<script src="_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js"></script>

应该是什么时候:

<script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>

【讨论】:

    猜你喜欢
    • 2020-03-07
    • 1970-01-01
    • 2020-07-29
    • 2021-07-09
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2021-06-05
    • 2020-05-09
    相关资源
    最近更新 更多