如何在 C# 连接字符串中隐藏密码?

发布于 2024-12-06 16:06:25 字数 276 浏览 0 评论 0原文

我有以下连接字符串:(

Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID=sa;Password=password

.net webservice) 显然,只需打开 app.config 文件并查看配置设置即可查看这一点。

我需要的是一种让黑客无法看到密码的方法。但同时,使其可自定义,以便在部署到另一个数据库时可以对其进行更改。

I have the following connection string:

Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID=sa;Password=password

(.net webservice)
This can obviously be viewed simply by opening up the app.config file and looking at the configuration settings.

What I need is a way to make a hacker unable to see the password. But at the same time, leave it customisable so that it can be changed when deployed on another database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一杆小烟枪 2024-12-13 16:06:25

您有许多选项 - 我所知道的选项(按优先顺序排列):

  1. 使用集成 (SSPI) 安全性,无需在配置文件中包含密码
  2. 加密连接字符串(请参阅 使用受保护的配置加密配置信息
  3. 单独存储用户名和密码,并使用字符串格式构建完整的连接字符串,

例如连接字符串可能看起来像这样:

Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID={0};Password={1}

我会选择选项 1,如果不可能,则选择选项 2。为了完整性,我已经提到了选项 3。

您是否阅读过保护连接信息 (ADO.NET)

You have a number of options - the ones that I am aware of (in order of preference):

  1. Use integrated (SSPI) security where you don't need to include a password in the config file
  2. Encrypt the connection string (see Encrypting Configuration Information Using Protected Configuration)
  3. Store the username and password separately and use string formatting to construct the full connection string,

So for example the connection string might look like this:

Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID={0};Password={1}

I'd go for option 1, if thats not possible then option 2. I've mentioned option 3 for completeness.

Have you read Protecting Connection Information (ADO.NET)?

俏︾媚 2024-12-13 16:06:25

首先,不要使用“SA”帐户。如果有人获得密码,您的数据库就会处于开放状态。使用仅允许对特定数据库执行 CRUD 操作的自定义帐户。

获取 web.config 的唯一方法是破解您的服务器。如果他们这么做了,你就完蛋了。

First of all, don't use the "SA" account. It leaves your database wide open if someone gets the password. Use a custom account which only is allowed to do CRUD operations on a specific database.

The only way to get web.config is to hack your server. And if they have done that, you're screwed anyway.

倚栏听风 2024-12-13 16:06:25

加密 web.config 或 app.config 中的连接字符串可能是最简单的方法,

请参阅如何操作:使用 DPAPI 加密 ASP.NET 2.0 中的配置节

Probably easiest to encrypt the connection strings within the web.config or app.config

See How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI

朦胧时间 2024-12-13 16:06:25

我建议加密/解密连接字符串。因此必须手动设置连接字符串。

对于加密,请看一下:
http://dotnet-snippets.de/dns/encrypt-and -decrypt-strings-SID205.aspx

对于自定义设置,请查看:
http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx

在运行时将 Encrypted 替换为正确的:

  public static void SetAppSettingValue(string Key, string Value)
   {

   System.Configuration.Configuration config == ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.

 config.AppSettings.Settings[Key].Value = Value;

  // Save the changes in App.config file.

   config.Save(ConfigurationSaveMode.Modified);

    ConfigurationManager.RefreshSection("appSettings");
  }

I Suggest en/decrypting the connection string. Therefore the connection string has to be set manually.

For encryption take a look at:
http://dotnet-snippets.de/dns/encrypt-and-decrypt-strings-SID205.aspx

For Custom Settings take a look at:
http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx

Replace the Encrypted with the correct one at runtime:

  public static void SetAppSettingValue(string Key, string Value)
   {

   System.Configuration.Configuration config == ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.

 config.AppSettings.Settings[Key].Value = Value;

  // Save the changes in App.config file.

   config.Save(ConfigurationSaveMode.Modified);

    ConfigurationManager.RefreshSection("appSettings");
  }
天涯离梦残月幽梦 2024-12-13 16:06:25

您可以加密连接字符串 - 然后当您访问连接字符串时,将其解密。但这并不是万无一失的,因为您随后会遇到在哪里存储用于解密连接字符串的密钥的问题!

You could encrypt the connection string - then when you access the connection string, decrypt it. This isn't fool proof though as you're then stuck with the problem of where to store the key to decrypt the connection string!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文