Encrypting web.config file

Discussion in 'ASP .Net' started by Alex. O. Koranteng, Dec 26, 2008.

  1. 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
     
    Alex. O. Koranteng, Dec 26, 2008
    #1
    1. Advertising

  2. 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
    se 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:
    .

    ==================================================
    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.
     
    Allen Chen [MSFT], Dec 29, 2008
    #2
    1. Advertising

  3. Hi Alex,

    Do you have any progress on this issue?

    Regards,
    Allen Chen
    Microsoft Online Support
     
    Allen Chen [MSFT], Jan 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -Steve-

    Encrypting web.config

    -Steve-, Aug 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    363
    -Steve-
    Aug 16, 2006
  2. Ollie Riches
    Replies:
    1
    Views:
    1,647
    Gregory A. Beamer
    Dec 4, 2008
  3. VR
    Replies:
    3
    Views:
    292
    Alek Davis
    Sep 9, 2003
  4. Lane
    Replies:
    3
    Views:
    386
    Dominick Baier [DevelopMentor]
    Apr 27, 2006
  5. Keith G Hicks
    Replies:
    0
    Views:
    150
    Keith G Hicks
    Apr 5, 2008
Loading...

Share This Page