使用Saml与Azure AD使用Blazer Web组件



我试图使用。net核心Blazer webAssembly客户端UI从main()和使用以下代码的SAML与Azure AD进行身份验证,但得到错误。下面的一个问题不能解决这个问题。如有任何提示,不胜感激

尝试登录时出错:'无法读取未定义的属性'redirectUri' '

OIDC工作正常

下面是我在Program.cs

中从Main调用的代码
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddHttpClient("BlazorWASMAuthApp.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
.CreateClient("BlazorWASMAuthApp.ServerAPI"));

builder.Services.AddApiAuthorization ();builder.Services.AddAuthenticationCore ();

builder.Services.AddAuthorizationCore ();
builder.Services.AddAuthentication().AddSaml2(options =>
{
// builder.Configuration.Bind("AzureAd", options.SPOptions.AuthenticateRequestSigningBehavior);
//APP Registration keys Will be moved to Json in subsequent story
options.SPOptions.EntityId = new EntityId("https://localhost:5001/saml2");
//options.SPOptions..Add("https://localhost:5001/saml2/API.Access");
options.IdentityProviders.Add(
new IdentityProvider(
new EntityId("https://sts.windows.net/{Clientid}/"), options.SPOptions)
{
//SingleSignOnServiceUrl = signinURI,
MetadataLocation = ...federationdataxml?clientid={clientid}"

});
;
})
.AddCookie();

根据ms文档,最常见的错误是由不正确的配置引起的

根据场景需求,可能缺少或不正确权限、实例、租户ID、租户域、客户端ID或重定向URI防止应用程序对客户端进行身份验证。

在与重定向中配置的端口不同的端口运行应用程序标识提供者的应用注册的URI。

请检查原因是否缺失返回url参数在你的代码配置中提供:

options.SPOptions.ReturnUrl = new Uri("https://localhost:5001/authentication/azurecallback");之后

options.SPOptions.EntityId = new EntityId("https://localhost:5001/saml2");

else checkPortal side配置。

注册API时,重定向uri设置为web,其字段为空,API从Expose API暴露。但是当注册客户端应用时,重定向uri必须设置,希望您已经这样做了。客户端应用>选择支持的帐号>重定向uri>选择SPA-给重定向url像https://localhost:5001/authentication/login-callback

(uri格式:https://localhost:{PORT}/authentication/login-callback.)

这个'authentication/login-callback'也必须出现在你的代码配置中。

从参考

注意:在Kestrel上运行的应用程序的默认端口是5001。如果如果应用运行在不同的红隼端口上,请使用该应用的端口。为IISExpress中可以找到应用程序随机生成的端口调试面板中服务器应用程序的属性。因为这个应用程序没有存在并且不知道IIS Express端口,返回到创建应用程序后执行此步骤,并更新重定向URI。一个注释出现在创建应用程序部分,以提醒IIS Express用户更新重定向URI.

所以检查端口运行,如果没有更新重定向uri。

引用:

参考1
  1. 参考2
  2. 歌珥

最新更新