Encrypting web.config file

  • Thread starter Thread starter Alex. O. Koranteng
  • Start date Start date
A

Alex. O. Koranteng

I am getting the error mesage indicated below when my code code hits the line
Configuration config = WebConfigurationManager.OpenWebConfiguration----
I am calling this class from a button on my page as shown below. Any
suggestions/help will be appreciated


using System;
using System.Web.Configuration;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EncDecWebConfig : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// load web.config into TextBox on first page visit...
if (!Page.IsPostBack)
{
RefreshWebConfig();
}
}

private void RefreshWebConfig()
{
// Read in the value of Web.config into the TextBox...
//using (StreamReader reader =
File.OpenText(Server.MapPath("~/Web.config")))
using (StreamReader reader =
File.OpenText(Server.MapPath("~/Web.config")))
{
webConfig.Text = reader.ReadToEnd();
}

// Programmatically read in the value of the connection string
PlainTextConnectionString.Text =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ConnectionString;
}

protected void btnRefreshWebConfig_Click(object sender, EventArgs e)
{
RefreshWebConfig();
}

protected void btnEncrypt_Click(object sender, EventArgs e)
{
ProtectSection("connectionStrings",
"DataProtectionConfigurationProvider");
RefreshWebConfig();
}


public void ProtectSection(string sectionName,
string provider)
{
Configuration config =
WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection section =
config.GetSection(sectionName);

if (section != null &&
!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
}


Server Error in '/EncConfig1' Application.
--------------------------------------------------------------------------------

Failed to map the path '/'.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Failed to map the path
'/'.

Source Error:


Line 73: string provider)
Line 74: {
Line 75: Configuration config =
Line 76: WebConfigurationManager.
Line 77: OpenWebConfiguration(Request
 
Hi Alex,

I tried your code and it works fine on my side. Could you try to create a
new project to test? Can it work?

If it still doesn't work could you send me the project? My email is
(e-mail address removed) update here after sending the project in case
I missed that email.

Regards,
Allen Chen
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Alex,

Do you have any progress on this issue?

Regards,
Allen Chen
Microsoft Online Support
 
Back
Top