【发布时间】:2021-03-14 07:53:30
【问题描述】:
我一直在关注此文档,了解如何在 blazor Web 应用程序中使用 Azure AD B2C 进行身份验证 https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-azure-active-directory-b2c?view=aspnetcore-5.0
遵循本文档后,我们得到了一个包含服务器和客户端的解决方案,两者都在 https 端口 5001 上运行。现在,我想切换到使用外部 api,而不是在端口上运行的5001.
当手动使用 blazor 检索到的访问令牌时,一切似乎都很好,并且身份验证成功。但是 blazor 只会自动将身份验证标头附加到以 https://localhost:5001 开头的请求。
当我改用 https://localhost:5003 时,身份验证标头为空。
有什么我可以添加到我的 MsalAuthentication 的提供程序选项中,以便它将此访问令牌传递给我在 https://localhost:5003 上运行的 api 吗?
builder.Services.AddHttpClient("{MyAssembly}.ServerAPI", client => client.BaseAddress = new Uri("https://localhost:5003"))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
// Supply HttpClient instances that include access tokens when making requests to the server project
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("{MyAssembly}.ServerAPI"));
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("https://{myproject}.onmicrosoft.com/e3b857b7-df50-4633-ae02-df4d4b20e911/API.Access openid offline_access");
});
【问题讨论】:
标签: azure blazor azure-ad-b2c msal