首页 文章

通过ConnectionString .NET连接到数据库的安全问题(内存转储)

提问于
浏览
1

我在使用包含密码的.net oracle数据库提供程序中的连接字符时遇到了一些安全问题 . 问题是那些提供程序(System.Data.OracleClient或ODAC)在String类型的参数中获取connectionString .

这意味着当我们将桌面应用程序直接连接到数据库(没有一些中间层服务)并且我们为此应用程序执行内存转储时,我们很可能从传递给数据库提供程序库的connectionString以纯文本形式获取用户和密码 .

据我所知,没有办法在SecureString类型参数中传递connectionString(google说只有MS SqlServer提供者允许这样做) .

在这种情况下是否还有其他方法来保护数据库密码(除了更改系统架构和创建用于连接到数据库的中间层服务而不是直接从应用程序连接)?

1 回答

  • 0

    默认情况下,Microsoft的SqlConnection将在连接后为您删除 .ConnectionString 中的密码:

    connection.ConnectionString = "Data Source=screwdriver;User ID=MrSnrub;Password=smithers;";
    connection.Open();
    
    >connection.ConnectionString
    "Data Source=screwdriver;User ID=MrSnrub;Password=smithers;"
    

    您可以通过指定 Persist Security Info=true; 来覆盖此行为:

    String cs = "Data Source=screwdriver;User ID=MrSnrub;Password=smithers;Persist Security Info=true";
    conn.ConnectionString = cs;
    conn.Open();
    
    >conn.ConnectionString   
    "Data Source=screwdriver;User ID=MrSnrub;Password=smithers;Persist Security Info=true"
    

    使用SecureString

    另一种方法是使用.NET 4.5的方法通过 SqlCredential 对象将密码作为 SecureString 传递给 SqlConnection

    SecureString pwd = AzureKeyVault.GetSettingSecure("DbPassword");
    SqlCredential cred = new SqlCredential("MrSnrub", pwd);
    
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=screwdriver;";
    conn.Credential = cred;
    

相关问题