Blazor WASM OIDC声明,无论我做什么,重定向URI都是未定义的

Blazor WASM OIDC stating that redirect uri is undefined whatever I do(Blazor WASM OIDC声明,无论我做什么,重定向URI都是未定义的)
本文介绍了Blazor WASM OIDC声明,无论我做什么,重定向URI都是未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我遵循了Microsoft Docs和这个Medium article中的指导原则,它们建议将OIDC身份验证添加到Blazor WASM应用程序应该是轻而易举的。

我已经有一个可以正常工作的OIDC-Server(Keyloak),我已成功地将其用于多个Blazor服务器应用程序。

然后我在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)启动后,我单击登录,出现以下消息:

那么无论我是定义RedirectUriappsetting.json还是在program.cs中显式定义RedirectUri

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

那么,我错过了什么?

推荐答案

我也遇到了这个问题。仔细检查添加了JS身份验证服务的index.html页面。

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

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

本应是:

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

这篇关于Blazor WASM OIDC声明,无论我做什么,重定向URI都是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)