|
| how can i encrypt and decrypt the app.config file connecting strings and appsettings in the windows applications |
 |
Thu, 3 Apr 2008 12:24:53 +0000 |
how to encrypt and decrypt the app.config file connecting strings and
appsettings in the vb.net
|
| Post Reply
|
| Re: how can i encrypt and decrypt the app.config file connecting strings and appsettings in the windows applications |
 |
Thu, 3 Apr 2008 12:39:27 +0000 |
Hi,
Some useful links:
http://msdn2.microsoft.com/en-us/library/ms998280.aspx
http://www.codeproject.com/KB/cs/Configuration_File.aspx
Code in VB.NETNamespace DT.Security
PublicClass UtilitiesPrivateSubNew()
EndSubPublicSharedSub ProtectConnectionString()
ToggleConnectionStringProtection(System.Windows.Forms.Application.ExecutablePath
, True)
EndSubPublicSharedSub UnprotectConnectionString()
ToggleConnectionStringProtection(System.Windows.Forms.Application.ExecutablePath
, False)
EndSubPrivateSharedSub ToggleConnectionStringProtection(ByVal pathName AsString,
ByVal protect AsBoolean)
' Define the Dpapi provider name.Dim strProvider AsString =
"DataProtectionConfigurationProvider"
' string strProvider = "RSAProtectedConfigurationProvider";
Dim oConfiguration As System.Configuration.Configuration = Nothing
Dim oSection As System.Configuration.ConnectionStringsSection = Nothing
Try
' Open the configuration file and retrieve the connectionStrings section.
' For Web!
' oConfiguration =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~&qu
ot;);
' For Windows!
' Takes the executable file name without the config extension.
oConfiguration =
System.Configuration.ConfigurationManager.OpenExeConfiguration(pathName)
IfNot oConfiguration IsNothingThen
Dim blnChanged AsBoolean = False
oSection = TryCast(oConfiguration.GetSection("connectionStrings"),
System.Configuration.ConnectionStringsSection)
IfNot oSection IsNothingThen
If (Not(oSection.ElementInformation.IsLocked)) AndAlso
(Not(oSection.SectionInformation.IsLocked)) Then
If protect Then
IfNot(oSection.SectionInformation.IsProtected) Then
blnChanged = True
' Encrypt the section.
oSection.SectionInformation.ProtectSection(strProvider)
EndIf
Else
If oSection.SectionInformation.IsProtected Then
blnChanged = True
' Remove encryption.
oSection.SectionInformation.UnprotectSection()
EndIf
EndIf
EndIf
If blnChanged Then
' Indicates whether the associated configuration section will be saved even if
it has not been modified.
oSection.SectionInformation.ForceSave = True
' Save the current configuration.
oConfiguration.Save()
EndIf
EndIf
EndIfCatch ex As System.Exception
Throw (ex)
Finally
EndTry
EndSubEndClass
EndNamespace
HTH,
Suprotim Agarwal
|
| Post Reply
|
|
|
|
|
|
|
|
|
|