简体   繁体   English

在 web.config 中加密 appSettings

[英]Encrypting appSettings in web.config

I am developing a web app which requires a username and password to be stored in the web.Config, it also refers to some URLs which will be requested by the web app itself and never the client.我正在开发一个 web 应用程序,它需要将用户名和密码存储在 web.Config 中,它还引用了一些将由 web 应用程序本身而不是客户端请求的 URL。

I know the .Net framework will not allow a web.config file to be served, however I still think its bad practice to leave this sort of information in plain text.我知道 .Net 框架不允许提供 web.config 文件,但是我仍然认为将此类信息以纯文本形式保留是不好的做法。

Everything I have read so far requires me to use a command line switch or to store values in the registry of the server.到目前为止我读过的所有内容都要求我使用命令行开关或将值存储在服务器的注册表中。 I have access to neither of these as the host is online and I have only FTP and Control Panel (helm) access.我无法访问这些,因为主机在线,我只能访问 FTP 和控制面板(掌舵)。

Can anyone recommend any good, free encryption DLL's or methods which I can use?任何人都可以推荐我可以使用的任何好的、免费的加密 DLL 或方法吗? I'd rather not develop my own!我宁愿不开发自己的!

Thanks for the feedback so far guys but I am not able to issue commands and and not able to edit the registry.感谢到目前为止的反馈,但我无法发出命令,也无法编辑注册表。 Its going to have to be an encryption util/helper but just wondering which one!它必须是一个加密实用程序/助手,但只是想知道是哪一个!

EDIT:编辑:
If you can't use asp utility, you can encrypt config file using SectionInformation.ProtectSection method.如果您不能使用 asp 实用程序,您可以使用SectionInformation.ProtectSection方法加密配置文件。

Sample on codeproject:代码项目示例:

Encryption of Connection Strings inside the Web.config in ASP.Net 2.0 ASP.Net 2.0 中 Web.config 中连接字符串的加密

While on the first glance it seems to be straightforward, there are a couple of hurdles I encountered.虽然乍一看似乎很简单,但我遇到了一些障碍。

So I am providing steps that worked fine for me (to encrypt the appSettings section) using the default crypto provider:所以我提供了使用默认加密提供程序对我有用的步骤(加密appSettings部分):

Encrypt sections in the web.config:加密web.config 中的部分:

  1. Open Admin command shell (run as administrator! ).打开管理命令外壳(以管理员身份运行 )。 The command prompt will be on C: which is assumed for the steps below.命令提示符将在C: ,假设为以下步骤。
    Further assumed is that the application is deployed on D:\\Apps\\myApp - replace this by the path you're using in step 3.进一步假设应用程序部署在D:\\Apps\\myApp - 将其替换为您在步骤 3 中使用的路径
  2. cd "C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319" , on 32 bit Windows systems use Framework instead of Framework64 cd "C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319" ,在 32 位 Windows 系统上使用Framework而不是Framework64
  3. cd /D "D:\\Apps\\myApp"
    Note: The /D switch will change the drive automatically if it is different from your current drive.注意:如果驱动器与当前驱动器不同, /D开关将自动更改驱动器。 Here it will change the path and drive, so the current directory will be D:\\Apps\\myApp afterwards.这里它会改变路径和驱动器,所以当前目录将是D:\\Apps\\myApp之后。
  4. c:aspnet_regiis -pef appConfig .

You should see this message:您应该会看到此消息:

Microsoft (R) ASP.NET RegIIS version 4.0.30319.0 Administration utility to install and uninstall ASP.NET on the local machine. Microsoft (R) ASP.NET RegIIS 版本 4.0.30319.0 管理实用程序,用于在本地计算机上安装和卸载 ASP.NET。 Copyright (C) Microsoft Corporation.版权所有 (C) 微软公司。 All rights reserved.版权所有。 Encrypting configuration section... Succeeded!加密配置部分...成功!

You can also Decrypt sections in the web.config: These are the same steps, but with option -pdf instead of -pef for aspnet_regiis.您还可以解密web.config 中的部分:这些是相同的步骤,但对于 aspnet_regiis,使用选项-pdf而不是-pef

It is also possible to encrypt other sections of your web.config, for example you can encrypt the connection strings section via:也可以加密 web.config 的其他部分,例如您可以通过以下方式加密连接字符串部分:

aspnet_regiis -pe "connectionStrings" -app "/SampleApplication"

More details about that can be found here.可以在此处找到有关此的更多详细信息


Note: The encryption above is transparent to your web application, ie your web application doesn't recognize that the settings are encrypted.注意:上述加密对您的 Web 应用程序是透明的,即您的 Web 应用程序无法识别设置已加密。
You can also choose to use non-transparent encryption, for example by using Microsoft's DPAPI or by using AES along with the Framework's AES Class .您还可以选择使用非透明加密,例如使用 Microsoft 的 DPAPI 或将AES与框架的AES Class一起使用。
How it is done with DPAPI I have described here at Stackoverflow .它是如何使用DPAPI完成的,我已经在 Stackoverflow 中进行了描述。 DPAPI works very similar in a sense that it uses the machine's or user credential's keys. DPAPI 的工作原理非常相似,因为它使用机器或用户凭据的密钥。 Generally, non-transparent encryption gives you more control, for instance you can add a SALT, or you can use a key based on a user's passphrase.通常,非透明加密为您提供更多控制权,例如您可以添加 SALT,或者您可以使用基于用户密码的密钥。 If you want to know more about how to generate a key from a passphrase, look here .如果您想了解有关如何从密码短语生成密钥的更多信息,请查看此处

使用 aspnet_setreg.exe http://support.microsoft.com/kb/329290

  1. Publish your project发布您的项目
  2. Open Developer command Prompt as Administrator以管理员身份打开开发人员命令提示符
  3. use this command asp_rigiis -pef "appSettings" "C:\\yourPublishPath" -prov "DataProtectionConfigurationProvider"使用此命令asp_rigiis -pef "appSettings" "C:\\yourPublishPath" -prov "DataProtectionConfigurationProvider"

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

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