gpt4 book ai didi

azure - 使用 azure AD B2C 进行 blazor Web api 身份验证

转载 作者:行者123 更新时间:2023-12-02 06:17:45 26 4
gpt4 key购买 nike

我一直在关注此文档,以在 blazor Web 应用程序中使用 Azure AD B2C 进行身份验证 https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-azure-active-directory-b2c?view=aspnetcore-5.0

遵循本文档后,我们得到了一个包含服务器和客户端的解决方案,两者都在 https 端口 5001 上运行。现在,我想切换到使用外部 api,而不是在端口上运行的 API 5001。

一切看起来都很好,并且手动使用 blazor 检索到的访问 token 时身份验证成功。但 blazor 仅自动将身份验证 header 附加到以 https://localhost:5001 开头的请求。

当我使用 https://localhost:5003 时,身份验证 header 留空。

是否可以将某些内容添加到我的 MsalAuthentication 的提供程序选项中,以便它将此访问 token 传递到在 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");
});

最佳答案

如果您想要向不在应用的基本 URI 内的 URI 发出传出请求,您可以创建一个自定义 AuthorizationMessageHandler 类来实现它。更多详情请引用here

例如

创建自定义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" });
}
}

Program.cs中添加以下代码。

using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace WebB2C
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");

builder.Services.AddScoped<CustomAuthorizationMessageHandler>();
builder.Services.AddHttpClient("ServerAPI", client =>
client.BaseAddress = new Uri("https://localhost:44389/"))
.AddHttpMessageHandler<CustomAuthorizationMessageHandler>();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
.CreateClient("ServerAPI"));

builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("https://<>.onmicrosoft.com/api/user_impersonation");
options.ProviderOptions.DefaultAccessTokenScopes.Add("openid");
options.ProviderOptions.DefaultAccessTokenScopes.Add("offline_access");
});

await builder.Build().RunAsync();
}
}
}

关于azure - 使用 azure AD B2C 进行 blazor Web api 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65106667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com