Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding values in web config dynamically? How To + Best way

i just came up with the reading, Writing and adding values dynamically in web.Config in asp.net. Have many ideas in mind, but i just wana know what is the best way of adding values in web config dynamically.

for example in my case i have to add

 <identity  userName="someDomain\User" password="password" impersonate="true" />

in

tag in web config from code behind.

waiting for Good responses

like image 207
Singleton Avatar asked Dec 21 '22 16:12

Singleton


1 Answers

I got you and the code you want is :

 public void saveIdentity(string username, string password, bool impersonate)
    {
        Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
        IdentitySection identitySection = (IdentitySection)objConfig.GetSection("system.web/identity");
        if (identitySection != null)
        {
            identitySection.UserName = username;
            identitySection.Password = password;
            identitySection.Impersonate = impersonate;
        }
    objConfig.Save();
}
like image 176
Marwan Avatar answered Dec 24 '22 04:12

Marwan