Click here to Skip to main content
15,302,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want change aouthentication mode in my project from code dinamically but cant save my change in web.config
my code for change is:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/HR");
        System.Web.Configuration.SystemWebSectionGroup systemWeb = (System.Web.Configuration.SystemWebSectionGroup)config.GetSectionGroup("system.web");
        System.Web.Configuration.AuthenticationSection section = (System.Web.Configuration.AuthenticationSection)systemWeb.Sections["authentication"];

        System.Web.Configuration.AuthenticationMode currentMode = section.Mode;
        section.Mode = System.Web.Configuration.AuthenticationMode.Windows;
if (!section.ElementInformation.IsLocked || !section.IsReadOnly())

and error is:
ConfigurationSection properties cannot be edited when locked.

please help me

1 solution

You need to use ConfigurationManager.OpenExeConfiguration in order to open a web.config file to make changes. Take a look at this post which sounds useful:
Read/Write App.Config File with .NET 2.0[^]

I hope it will help,
abtinC# 2009 8-Apr-12 7:16am
Tanx alot
my project is web site so i can't use ConfigurationManager.OpenExeConfiguration.
I can change my config properties but when save new config, project must be restarted ,that i can't this.

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