[英]Using Saml with Azure AD using Blazer Web assembly
我正在尝试从 main() 使用 .NET 核心 Blazer webAssembly 客户端 UI,并使用 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 中配置的端口不同的端口上运行应用程序。
请检查原因是否在您提供的代码中的配置中缺少return url 参数:
options.SPOptions.ReturnUrl = new Uri("https://localhost:5001/authentication/azurecallback");
之后
options.SPOptions.EntityId = new EntityId("https://localhost:5001/saml2");
否则检查门户网站端配置。
注册 API 时,重定向 uri 设置为 web,其字段留空,api 从 Expose api 中暴露出来。但是在注册客户端应用程序时,必须设置重定向 uri,希望你已经这样做了。 客户端应用程序 > go 活动目录 b2c > 命名 > 选择支持的帐户 > 重定向 uri > 选择SPA - 给你重定向 https://localhost:5001/authentication/login-callback
(uri 的格式:https://localhost:{PORT}/authentication/login-callback。)
此“身份验证/登录回调”也必须存在于您的代码配置中。
来自参考
注意:在 Kestrel 上运行的应用程序的默认端口是 5001。如果应用程序在不同的 Kestrel 端口上运行,请使用该应用程序的端口。 对于 IIS Express,可以在调试面板的服务器应用程序属性中找到应用程序随机生成的端口。 由于此时应用程序不存在且 IIS Express 端口未知,因此在创建应用程序后返回此步骤并更新重定向 URI。 Create the app 部分会出现一条备注,提醒 IIS Express 用户更新重定向 URI 。
因此,检查正在运行的端口并更新重定向 uri(如果未完成)。
参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.