如何从多个应用程序集中提取和保护相同的连接字符串?

How do I abstract and secure the same connection string from many applications centrally?

我在同一台服务器上有多个 Web 应用程序 运行。它们都将数据库服务器的内部 IP 地址以及数据库的用户名和密码硬编码到它们的 Web.config 文件中。我正在执行许多服务器升级,并且必须遍历所有这些应用程序并更新各处的连接字符串。我想要一个集中且安全的地方来存储连接字符串。

我可以在服务器上放置一个守护进程或应用程序,它可以安全地包含数据库的连接字符串详细信息,为我的应用程序提供一个 'virtual' 连接字符串以指向,并通过隧道返回请求来来回回?

遗留应用程序是 运行 LINQ-to-SQL,其中一些已更新为使用一些 Entity Framework。

您可以使用:

  1. 用于更新 web.config 文件的 PowerShell(下面的一些示例代码可帮助您入门)
  2. Web.deploy https://msdn.microsoft.com/en-us/library/vstudio/dd465318(v=vs.100).aspx

$dbServer = ReadHostIfNull $dbServer "Enter name of database server" $dbName = ReadHostIfNull $dbNameSAM "Enter name of SAM database" $dbUserName = ReadHostIfNull $dbUserName "Enter SQL authentication username for database (leave blank for integrated security)"

$connectionString = "Data Source=$dbServer;Initial Catalog=$dbName;$securityPart"

Write-Output "Modifying web.config..."
if($webConfigPath -eq "")
{
    $configFile = Get-WebConfigFile "IIS:/Sites/$webSite/$vDirSAM"
    $webConfigPath = $configFile.FullName
}

$configXml = [xml](Get-Content $webConfigPath)

UpdateAppSetting $configXml "ConnectionString" $connectionString

function UpdateAppSetting
{ param ($configXml, $key, $newValue)
    $configXml.SelectSingleNode("//configuration/appSettings/add[@key = ""$key""]").value = "$newValue"
}