简体   繁体   English

使用Entity Framework时,如何将数据库用户名和密码安全地存储在web.config文件中

[英]How can a database username and password be stored securely in a web.config file when using Entity Framework

When using Entity Framework to access a database on a non-local server how should I be specifying the username and password parameters in the connection string (which is stored in the web.config file)? 使用Entity Framework访问非本地服务器上的数据库时,如何在连接字符串中指定用户名和密码参数(存储在web.config文件中)?

I've read in a C# step by step guide(2010 edition by John Sharp) to never hard code them into your application due to potential reverse engineering or if someone gets hold of the source code. 我已经阅读了C#分步指南(John Sharp 2010版),因为潜在的逆向工程或有人掌握了源代码,所以从不硬编码到您的应用程序中。 So I would like to know the conventional best practice for doing so. 所以我想知道这样做的传统最佳实践。

Hard coding the user and password is bad for 3 reasons: 硬编码用户和密码有三个原因:

  • it gives you a false impression of security, because when you look at a binary file with a text editor you don't understand anything but in fact it's a piece of cake to disassemble a .NET assembly 它给你一个安全的错误印象,因为当你用文本编辑器查看二进制文件时你什么都不懂,但实际上拆卸.NET程序集是件小事
  • it forces all software developers to be allowed to know the user and password 它迫使所有软件开发人员知道用户和密码
  • it implies that a change of the user/password pair requires a new deploy which also include the recompiling of the application 这意味着更改用户/密码对需要新的部署,其中还包括重新编译应用程序

There is no magic solution to this problem and security in this case is only as good as the discipline and good will of the people in charge with the security. 这个问题没有神奇的解决方案,在这种情况下,安全性只能与负责安全的人员的纪律和善意一样好。

In my company, it goes something like this: 在我的公司,它是这样的:

  • software developers don't have access to the production database, and most certainly they don't know the user and password 软件开发人员无权访问生产数据库,而且他们肯定不知道用户和密码
  • software administrators have the username and password and they merge the web.config comming from the development department with their own secrets when deploying the application on the production machines 软件管理员拥有用户名和密码,当他们在生产机器上部署应用程序时,他们将开发部门提交的web.config与他们自己的秘密合并
  • no other person has access to the production machines appart from the software administrators 没有其他人可以从软件管理员访问生产机器appart

Encrypting the user and password in the web.config can only help you so much. 加密web.config的用户和密码只会对你有所帮助。 Eventually you'll have to hard code the encryption key, in clear form, in your application and that takes us back to the disassembling problem. 最终,您必须在应用程序中以清晰的形式对加密密钥进行硬编码,这会将我们带回到反汇编问题中。

In my opinion, a very good solution would be a combination of what's going on in my company and encryption with a clear key and obfuscation. 在我看来,一个非常好的解决方案是将我公司发生的事情和加密与明确的密钥和混淆相结合。

The general idea is: 一般的想法是:

  • take what I said about the application administrator guys 拿我说的关于应用程序管理员的人
  • modify one minor detail: they don't know the clear username and password, they know an encrypted form 修改一个小细节:他们不知道明确的用户名和密码,他们知道加密的表格
  • only the dev guys have the key to decrypt the encrypted username and password and they use that at runtime 只有开发人员有密钥才能解密加密的用户名和密码,并且他们在运行时使用它
  • the dev guys should obfuscate their assemblies so that it's not worth it for anyone to try to reverse engineer the binaries, find out the clear key, somehow ask the application administrators what the encrypted username and password is (while drinking bear in a work outing) and then put everything together 开发人员应该对他们的程序集进行模糊处理,以便任何人都不值得尝试对二进制文件进行逆向工程,找出明确的密钥,以某种方式向应用程序管理员询问加密的用户名和密码是什么(在工作场所中喝酒时)然后把所有东西放在一起

That means that someone (maybe the owner of the company or some other head) needs to use a "greasemonkey" app to encrypt usernames and passwords and give the resulting encryptions to the application administrators. 这意味着某人(可能是公司的所有者或其他负责人)需要使用“greasemonkey”应用程序来加密用户名和密码,并将结果加密给应用程序管理员。

Don't forget there's also the db administrators which initially gave the owner an initial pair of credentials. 不要忘记还有数据库管理员最初给所有者一个初始的凭证对。 The owner needs to change the password and then do everything I laid out. 所有者需要更改密码,然后完成我布置的所有内容。

In conclusion, there are many solutions, some wackier than others. 总之,有许多解决方案,有些比其他解决方案更狡猾。 It's not all in the tools and code but also in the discipline. 它不仅仅在工具和代码中,而且在学科中。

You can encrypt sections of your web.config. 您可以加密web.config的各个部分。 See this walkthrough on MSDN: http://msdn.microsoft.com/library/dtkwfdky.aspx It's pretty simple to follow. 请参阅MSDN上的演练: http//msdn.microsoft.com/library/dtkwfdky.aspx这很容易理解。

You can try with following code, 您可以尝试使用以下代码,

ConnectionStringsSection oSection = Configuration.ServiceConfiguration.GetConnectionStrings();
    if(!oSection.SectionInformation.IsLocked && !oSection.SectionInformation.IsProtected)
    {
        oSection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); 
        oSection.CurrentConfiguration.Save();
    }

EDIT: 编辑:

you can get more information on Protected configuration from MSDN Link, http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx 您可以从MSDN Link获取有关受保护配置的更多信息, http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx

You can encrypt a particular section of a web.config file easily using the method shown in this document: Encrypting and Decrypting Configuration Sections 您可以使用本文档中显示的方法轻松加密web.config文件的特定部分: 加密和解密配置部分

It is transparent for your application code and the encrypted section is useless outside the machine it was encrypted on. 它对您的应用程序代码是透明的,并且加密的部分在加密的机器外部是无用的。

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

相关问题 funnelweblog的asp.net web.config文件中如何设置用户名和密码? - how to set the username and password in the asp.net web.config file of funnelweblog? 从web.config检索用户名和密码 - retrieve username and password from web.config 实体框架无法在Web.config中找到连接字符串 - Entity Framework Can't Find Connection String in Web.config 使用Entity Framework CodeFirst从Web.config中删除ConnectionString - Remove ConnectionString from Web.config using Entity Framework CodeFirst 如何在web.config中更改实体框架的连接字符串 - How to change connection string in web.config for entity framework 如何将存储在 web.config 中的值添加到 c# 中的类属性(来自 web 配置文件而不是资源文件) - How can I add a value that is stored in web.config to a class attribute in c#(from web config file not the resource file) 如何保护存储在 web.config 中的密码? - How can I secure passwords stored inside web.config? 在C#中,如何读取存储在web.config文件连接字符串中的连接字符串? - In C# , how can I read a connection string stored in my web.config file connection string? 如何显示使用JavaScript在web.config文件的appsettings中存储的值? - how to display a value that is stored in appsettings of web.config file using javascript? 如何在C#中与web.config分开读取用户名和密码 - How to read username and password separately from web.config in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM