简体   繁体   English

存储设置的最佳实践

[英]Best practice for storing settings

I have only been programming for the better part of 1-2 years, C# the last 7 months or so, Up til now I have used the .config file to store needed settings that cannot be stored in the database, and it was okay to do so. 我只在1-2年的大部分时间里才进行编程,最近7个月左右才使用C#,直到现在,我一直使用.config文件来存储无法存储在数据库中的所需设置,并且可以这样做。

Now I have a client where there are many users that will access a database, and part of the spec is that the application must log into sql using the sa username, obviously if anyone gets hold these settings it would be a problem. 现在,我有一个客户端,其中有许多用户将访问数据库,并且规范的一部分是应用程序必须使用sa用户名登录sql,显然,如果有人持有这些设置,那将是一个问题。

I want to know what the best practice for something like this would be, I can encrypt the password and server address, but I still feel uneasy about this. 我想知道这样的最佳做法是什么,我可以加密密码和服务器地址,但是对此我仍然感到不安。

What is the best practice in the industry for storing settings that cannot be in the database, especially the ones that are sensitive configurations 在业界最好的做法是存储数据库中无法存在的设置,尤其是那些敏感配置的设置

Many Thanks in advance 提前谢谢了

The best practice method (I think) would be to use SQL server integrated security: (you authorize a Windows/Active Directory user to access the server, the security aspect is now handled by Windows and your Domain configuration) - however this is not always practical, and you might not want to give the windows user that much access to the database outside the client software (eg, the application must insert/update/delete records in the database, something you wouldn't necessarily want the user to be able to do if they logged into the DB via SSMS) 最佳实践方法(我认为)是使用SQL Server集成安全性:(您授权Windows / Active Directory用户访问服务器,现在由Windows和您的域配置来处理安全性)-但这并非总是如此实用,并且您可能不希望Windows用户在客户端软件之外获得对数据库的大量访问权限(例如,应用程序必须在数据库中插入/更新/删除记录,而您不一定希望用户能够如果他们通过SSMS登录到数据库,该怎么办)

Another method would be to use the 'Protect' and 'Unprotect' methods of the System.Security.Cryptography.ProtectedData class to encrypt and decrypt the password, and/or the connection string. 另一种方法是使用System.Security.Cryptography.ProtectedData类的'Protect'和'Unprotect'方法来加密和解密密码和/或连接字符串。 (you will need to set a reference to System.Security.dll). (您将需要设置对System.Security.dll的引用)。

This gets around the "where do you hide the key" issue - the ProtectedData class uses your Windows machine's entropy pool to generate a key. 可以解决“您在哪里隐藏密钥”的问题-ProtectedData类使用Windows计算机的熵池生成密钥。 You can add your own salt (by way of a byte-array as "additional entropy") to ensure that the data cannot be retrieved by another .NET program running under the same user-context also using the ProtectedData class. 您可以添加自己的盐(通过字节数组作为“附加熵”),以确保使用相同的用户上下文运行的另一个.NET程序也不能使用ProtectedData类来检索数据。 You can 'protect' the password/data so it can only be 'un-protected' by the same user on the same machine that protected it. 您可以“保护”密码/数据,这样它只能由保护它的同一台计算机上的同一用户“取消保护”。

Hope this helps :) 希望这可以帮助 :)

Cheers, 干杯,

Simon. 西蒙

You can put settings into a local database. 您可以将设置放入本地数据库。 I prefer MS SQL Server Compact 3.5, which is free. 我更喜欢免费的MS SQL Server Compact 3.5。 This way you can store your settings into a local SDF database file, which can be encrypted and password protected. 这样,您可以将设置存储到本地SDF数据库文件中,该文件可以进行加密和密码保护。 The data stored in the SDF file can be accessed using eg ADO.NET+SQL, but I prefer Linq-to-SQL. 可以使用ADO.NET + SQL访问存储在SDF文件中的数据,但是我更喜欢Linq-to-SQL。

EDIT: 编辑:

Please take into account that although SDF files can be encrypted and password protected, if the file is stolen, it can definitely be cracked by a brute force method. 请注意,尽管可以对SDF文件进行加密和密码保护,但如果文件被盗,则肯定可以通过蛮力破解。 The same is true for any other solution, which stores sensitive data on client machines. 对于任何其他将敏感数据存储在客户端计算机上的解决方案,也是如此。

您可以尝试将信息存储在隔离存储中

I can't recommend highly enough to revisit the requirement to have the sa account used by the application. 我不能提出足够高的建议来重新考虑使用该应用程序使用sa帐户的要求。 That is a HUGE security hole. 那是一个巨大的安全漏洞。 Given the information provided, I would recommend encrypting the connection (won't really matter where you store it). 给定提供的信息,我建议对连接进行加密(在存储它的位置并不重要)。 Make sure that if your database connection methods fail, the error result won't display the user name and password. 确保如果您的数据库连接方法失败,则错误结果将不会显示用户名和密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM