在Azure Key Vault中存储SQL数据库连接字符串

时间:2018-02-08 19:53:59

标签: asp.net connection-string azure-virtual-machine azure-keyvault app-secret

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

是否有任何模式和最佳做法可以帮助完成此迁移?我计划使用连接字符串名称作为密码名称,连接字符串值作为密码值。但是,我不确定如何处理连接字符串提供程序名称。

另外,理想情况下,只有密钥保险库网址会出现在web.config中,但是如果当前引用实际连接字符串,则web.config中的现有配置如何与密钥保管库集成。

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>

1 个答案:

答案 0 :(得分: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;

    /* ... */
}
相关问题