2011-06-09 52 views
12

我的Web.config中有以下條目,我使用.NET 2.0和C#進行編碼。如何在C#2.0中的Web.config中加密用戶名和密碼

<add key="userName" value="s752549"/> 
<add key="userPassword" value="[email protected]"/> 

現在我希望對此進行加密以避免任何人看到它,並且這些密碼可能會頻繁更改(每隔15天)。

+1

可能重複的命令[加密的Web.Config](http://stackoverflow.com/questions/1075245/encrypting-web-config) – 2011-06-09 10:33:30

+0

另外複製的HTTP:/ /stackoverflow.com/questions/54200/encrypting-appsettings-in-web-config – 2011-06-09 10:34:29

+0

可能重複[在web.config中加密appSettings](https://stackoverflow.com/questions/54200/encrypting-appsettings-in-web -config) – 2017-06-29 14:48:21

回答

9

你可以把用戶名和密碼到單獨的部分,只有加密這個部分。例如:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
     <section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
    </configSections> 

    <appSettings> 
     <add key="Host" value="www.foo.com" /> 
     <add key="Token" value="qwerqwre" /> 
     <add key="AccountId" value="123" /> 
     <add key="DepartmentId" value="456" /> 
     <add key="SessionEmail" value="[email protected]" /> 
     <add key="DefaultFolder" value="789" /> 
    </appSettings> 

    <secureAppSettings> 
     <add key="userName" value="s752549"/> 
     <add key="userPassword" value="[email protected]"/> 

    </secureAppSettings> 
</configuration> 

然後用ASPNET_REGIIS

For Ex: 
aspnet_regiis -pef secureAppSettings . -prov DataProtectionConfigurationProvider 
+0

如何在aspnet_regiis中給出webconfig的路徑 – 2011-06-09 12:00:05

+0

同時你也可以讓我知道如何獲得我們.net代碼中的加密值。 – 2011-06-09 12:11:02

+0

檢查這爲更詳細地: http://diablopup.blogspot.com/2007/04/aspnetregiis-encryptdecrypt-webconfig.html 得到vlaues這樣 字符串username = ConfigurationSettings.AppSettings [ 「userName的」]; – Saurabh 2011-06-09 12:23:49

10

只想添加到這一點,標誌着答案是完成了99%,但它並沒有提供如何指定網絡配置的位置。我認爲我只是發佈完整的命令,而不是紮根於互聯網。因此,這裏是我的執行

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>aspnet_regiis -pef "secureAppSettings" "C:\MyLocalPublishDirectory\MyApp" -prov DataProtectionConfigurationProvider