gpt4 book ai didi

azure - 如何在没有应用程序设置部分的情况下使用 AddMicrosoftIdentityWebApiAuthentication?

转载 作者:行者123 更新时间:2023-12-02 22:50:40 26 4
gpt4 key购买 nike

我正在 .NET 5 API 中实现 Azure Active Directory。我目前这个 API 在 .NET Core 2.2 上完美运行。

这是旧的工作代码:

services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options =>
{
options.Instance = "https://login.microsoftonline.com/";
options.Domain = backOfficeADDomain;
options.TenantId = backOfficeADTenantId;
options.ClientId = $"api://{backOfficeADAPIClientId}";
options.ClientSecret = backOfficeADAPISecret;
});

但自从更新到 .NET 5 后,我收到此警告:

'AzureADAuthenticationBuilderExtensions.AddAzureADBearer(AuthenticationBuilder, Action)' is obsolete: 'This is obsolete and will be removed in a future version. Use AddMicrosoftWebApiAuthentication from Microsoft.Identity.Web instead. See https://aka.ms/ms-identity-web.'

所以我尝试将其更新为:

services.AddMicrosoftIdentityWebApiAuthentication(_configuration, "AzureAd");

appsettings.json 中的“AzureAd”部分似乎是传递凭据的唯一方法。如何手动输入实例、域、ClientId 等?我不使用 appsettings.json,所有数据都是从 AzureKeyVault 手动检索的。

谢谢!

最佳答案

假设您有充分的理由不使用设置中的配置值,则可以添加内存提供程序。

您还可以创建仅用于此扩展方法的配置:

var azureAdConfig = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"AzureAd:Instance", "https://login.microsoftonline.com/"},
{"AzureAd:Domain", backOfficeADDomain}
//...
})
.Build();

services.AddMicrosoftIdentityWebApiAuthentication(azureAdConfig);

关于azure - 如何在没有应用程序设置部分的情况下使用 AddMicrosoftIdentityWebApiAuthentication?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66477690/

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