protected void btnAdd_Click(object sender, EventArgs e) { AuthorizationSection section; System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath); section = (AuthorizationSection)config.GetSection("system.web/authorization"); AuthorizationRule arule = new AuthorizationRule(AuthorizationRuleAction.Allow); string[] strAllowedUsers = GetUsers(); section.Rules.Clear(); foreach (string element in strAllowedUsers) { arule.Users.Add(element); } arule.Users.Add(txtAddUser.Text.ToString()); section.Rules.Add(arule); config.Save(); } private string[] GetUsers() { string[] strUser = null; try { AuthorizationSection section; System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath); section = (AuthorizationSection)config.GetSection("system.web/authorization"); var rules = section.Rules; var allowedUsers = rules .OfType<authorizationrule>() .Where(r => r.Action == AuthorizationRuleAction.Allow) .Select(r => r.Users).First(); if (allowedUsers.ToString() != null) strUser = allowedUsers.ToString().Split(','); } catch { } return strUser; }
Unable to save config to file 'c:\inetpub\wwwroot\myapplication\web.config
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)