使用azure AD B2C进行blazor web api身份验证
在blazor web应用程序中使用Azure AD B2C进行身份验证时,我一直遵循此文档 在完成本文档之后,我们将得到一个包含服务器和客户端的解决方案,两者都运行在https端口5001上。现在,我想切换到使用外部api,而不是在端口5001上运行的api 当手动使用blazor检索到的访问令牌时,一切看起来都很好,身份验证成功。但是blazor只会自动将身份验证头附加到以https://localhost:5001. 当我使用https://localhost:5003,则验证标头保留为空 有什么我可以添加到我的MsalAuthentication的provider选项中,以便它将这个访问令牌传递给我在上运行的api吗https://localhost:5003?使用azure AD B2C进行blazor web api身份验证,azure,blazor,azure-ad-b2c,msal,Azure,Blazor,Azure Ad B2c,Msal,在blazor web应用程序中使用Azure AD B2C进行身份验证时,我一直遵循此文档 在完成本文档之后,我们将得到一个包含服务器和客户端的解决方案,两者都运行在https端口5001上。现在,我想切换到使用外部api,而不是在端口5001上运行的api 当手动使用blazor检索到的访问令牌时,一切看起来都很好,身份验证成功。但是blazor只会自动将身份验证头附加到以https://localhost:5001. 当我使用https://localhost:5003,则验证标头保留为
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");
});
builder.Services.AddHttpClient(“{MyAssembly}.ServerAPI”),client=>client.BaseAddress=newURI(“https://localhost:5003"))
.AddHttpMessageHandler();
//向服务器项目发出请求时,提供包含访问令牌的HttpClient实例
builder.Services.addScope(sp=>sp.GetRequiredService().CreateClient(“{MyAssembly}.ServerAPI”);
builder.Services.AddMsalAuthentication(选项=>
{
builder.Configuration.Bind(“AzureAdB2C”,options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add(“https://{myproject}.onmicrosoft.com/e3b857b7-df50-4633-ae02-df4d4b20e911/API.Access openid脱机访问”);
});
如果您想向不在应用程序基本URI内的URI发出传出请求,可以创建一个自定义的AuthorizationMessageHandler
类来实现它。有关详细信息,请参阅
比如说
创建自定义AuthorizationMessageHandler类
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
{
public CustomAuthorizationMessageHandler(IAccessTokenProvider provider,
NavigationManager navigationManager)
: base(provider, navigationManager)
{
ConfigureHandler(
authorizedUrls: new[] { "https://localhost:44389/" },
scopes: new[] { "https://<>.onmicrosoft.com/api/user_impersonation" });
}
}
如果要向应用程序的基本URI之外的URI发出传出请求,可以创建一个自定义
AuthorizationMessageHandler
类来实现它。有关详细信息,请参阅
比如说
创建自定义AuthorizationMessageHandler类
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
{
public CustomAuthorizationMessageHandler(IAccessTokenProvider provider,
NavigationManager navigationManager)
: base(provider, navigationManager)
{
ConfigureHandler(
authorizedUrls: new[] { "https://localhost:44389/" },
scopes: new[] { "https://<>.onmicrosoft.com/api/user_impersonation" });
}
}