Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
using System;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for clsEncrypt
/// </summary>
public class clsEncrypt
{
    //public clsEncrypt()
    //{
        //
        // TODO: Add constructor logic here
        //
    //}
   public static object webencrypt()
{
	Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
	//' Write the section name of web.config file (connectionStrings)
    ConfigurationSection configSection = config.GetSection("connectionStrings"); ;
	

	//' Check the section  of web.config file (connectionStrings) if Protected 
	//' then UnprotectSection (decrypt) the section
	if (configSection.SectionInformation.IsProtected) 
    {
		configSection.SectionInformation.UnprotectSection();
		config.Save();
	} 
    else
    {
		//' Check the section of web.config file (connectionStrings) 
		//' if UnprotectSection then 
		//' protect (encrypt) Section the section
        configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
		config.Save();
	}
    return config;
}

    
}



i want to encrypt my web.config file .But when a'm try to encrypt web.config file using this code it giving errors like "An error occurred loading a configuration file: Failed to map the path '/'."
please someone provide me good soluation for this ............
thanks in adv.
Posted
Updated 17-Feb-12 1:21am
v2
Comments
Ed Nutting 17-Feb-12 7:25am    
Your error is that it can't find the file to encrypt, not that the rest of the code doesn't work. You need to give it a proper file name and a file name that actually points to a file - at the moment the error is saying it can't actually "map the path" - make a proper file name - out of whatever you are passing it. Only you can solve this by checking your file system and what your file name should be.

1 solution

This is probably because the user in which context the application runs (ISS) has not enough permission to change and/or save web.config.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900