Azure AD 配置 Azure App Service 应用注册 .NET 7

问题描述 投票:0回答:0

我试图了解在部署到 Azure 应用服务时如何正确配置 Azure AD 配置作为概念证明。

我的具体用例是 Blazor Server,但据我所知,到目前为止我进行的研究表明这是一个一般的 ASPNET.Core 问题。

我已经为本地运行/测试创建了一个独立的应用程序注册,配置如下:

用户秘密:

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "xxxxx.onmicrosoft.com",
    "TenantId": "xxxxxxxxx",
    "ClientId": "xxxxxxxxx",
    "ClientSecret": "xxxxxxxxx",
    "ClientCertificates": [
    ],
    "CallbackPath": "/signin-oidc"
  }

我在Program.cs中的配置代码如下:

var initialScopes = builder.Configuration["DownstreamApi:Scopes"]?.Split(' ');

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
        .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
            .AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
            .AddInMemoryTokenCaches();
builder.Services.AddControllersWithViews()
    .AddMicrosoftIdentityUI();

创建 Azure 应用服务时会生成自动应用注册,但我很难理解如何在我的应用程序中的这种情况下使用它。

我还使用 KeyVault 来检索 Azure 存储帐户连接字符串,在我尝试让事情正常运行的过程中,我注意到我在部署时遇到的一个错误是它试图使用与关联的应用程序注册的客户端 ID 访问我的 KeyVault在 Azure 门户中创建 Web 应用程序。它似乎也创建了一个关联的客户端密码,但我当然无法完整地看到它,因为它被混淆了。

向 KeyVault 添加必要的权限允许检索连接字符串。

到目前为止,我已经成功地使用现有的独立应用程序注册并将“AzureAd”配置存储在 appsettings.json 中。唯一例外的是ClientSecret,它单独存放在App Service的ConfigurationSettings中,但就是感觉不是正确的做法。

我认为我的理解中缺少一块,所以欢迎任何指点。

c# asp.net-core azure-active-directory azure-web-app-service blazor-server-side
© www.soinside.com 2019 - 2024. All rights reserved.