如何使用服务主体/托管标识访问Azure应用配置?

qf9go6mv  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(163)

我需要禁用Azure应用配置存储的本地身份验证方法(访问密钥)。当前对于ASP.NET Framework应用程序,我使用以下方法从我的应用程序访问应用配置存储:

<configSections>
    <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>

<configBuilders>
    <builders>
        <add name="MyConfigStore" mode="Greedy" connectionString="${ConnectionString}" type="Microsoft.Configuration.ConfigurationBuilders.AzureAppConfigurationBuilder, Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration" />
        <add name="Environment" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
    </builders>
</configBuilders>

这里${ConnectionString} = "Endpoint=https://<app_config>.azconfig.io;Id=<Id>;Secret=<Access Key>"的值
现在,为了通过ASP.NET应用程序访问应用程序配置,我创建了一个服务主体,生成了一个要使用的密码。
我已存储了CLIENT_IDTENANT_IDCLIENT_SECRET值。我还将应用程序配置数据读取器角色分配给了服务主体。
我还有一个可以使用的托管身份。
现在,为了通过ASP.NET应用程序访问应用程序配置,我需要在应用程序端进行什么更改?

jhdbpxl9

jhdbpxl91#

检查以下解决方法以访问.NET Framework应用程序中的App Configuration
Azure Portal =〉App Configuration =〉Configuration explorer中,创建新的Key-value

  • 安装以下NuGet Packages
Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguratio - Version 1.0.0
Microsoft.Configuration.ConfigurationBuilders.Environment - Version 2.0.0
System.Configuration.ConfigurationManager - Version 7.0.0

Web.config文件中的配置节:

<configSections>
    <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
  </configSections>

  <configBuilders>
    <builders>
      <add name="AzureAppConfig" mode="Greedy" connectionString="Endpoint=https://AppConfigName.azconfig.io;Id=XqdS-l2-s0:****/;Secret=****" type="Microsoft.Configuration.ConfigurationBuilders.AzureAppConfigurationBuilder, Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration" />
      <add name="Environment" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
    </builders>
  </configBuilders>

  <appSettings configBuilders="Environment,AzureAppConfig">
    <add key="AppName" value=".NET Framework Sample" />
  </appSettings>
    • 正在读取配置值:**

在控制器中,

public ActionResult Index()
        {
            string FromAppConfig = System.Configuration.ConfigurationManager.AppSettings["TestApp:Settings:Message"];
            string FromWebConfig = System.Configuration.ConfigurationManager.AppSettings["AppName"];
            ViewBag.FromAppConfig = FromAppConfig;
            ViewBag.FromWebConfig = FromWebConfig;
            return View();
        }

在视图中. cshtml:

@{
    ViewBag.Title = "Home Page";
}

<div>
    <h2>  Value from App Configuration - @ViewBag.FromAppConfig</h2>
    <h2>  Value from Web.Config File - @ViewBag.FromWebConfig</h2>
</div>

输出:

引用自MSDoc

相关问题