0

我正在将应用程序机密从源代码控制的 web.config(带有转换)移动到 Azure Key Vault 机密。这对于密码之类的东西很有效,但是 SQL 数据库连接字符串似乎很难从 web.config 中移出。在 web.config 的其他地方(例如角色提供者、成员提供者、配置文件提供者、实体框架等)有各种对连接字符串的引用。

是否有任何模式和最佳实践来帮助进行此迁移?我打算将连接字符串名称用作秘密名称,将连接字符串值用作秘密值。但是,我不确定如何处理连接字符串提供程序名称。

此外,理想情况下,只有密钥保管库 URL 会在 web.config 中,但是如果 web.config 中的现有配置当前正在引用实际的连接字符串,那么它们如何与 Key Vault 集成。

IIS 托管的 ASP.NET 应用程序当前在 Azure 虚拟机中运行,但还必须考虑本地开发。

更新 其他库/包添加和使用的如下配置呢?

<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider"
         type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         connectionStringName="DefaultConnection" />
  </providers>
</sessionState>
<profile defaultProvider="DefaultProfileProvider">
  <providers>
    <add name="DefaultProfileProvider"
         type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider"
         type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
         enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
         minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider"
         type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</roleManager>
4

1 回答 1

1

想出了一个解决方案:将连接字符串保留在 web.config 中,因为它们被 web.config 的其他部分引用,但在应用程序启动时更新值。

protected void Application_Start()
{
    /* ... */

    var configurationReadOnlyField = typeof(ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

    var connection = ConfigurationManager.ConnectionStrings[connectionName];
    s_configurationReadOnlyField.SetValue(connection, false);

    connection.ConnectionString = newValue;

    /* ... */
}
于 2018-02-21T19:23:15.917 回答