title: How can I add and remove authorised users from web.config location path in asp.net
body: <p>I am trying to add and remove authorised users from web.config programmatically. I am using Windows Authentication.</p>
  
  <p>This is what I have on web.config</p>
  
  <pre><code>  &lt;location path="Admin"&gt;
      &lt;system.web&gt;
        &lt;authorization&gt;
          &lt;allow users="domain\user1, domain\user2"/&gt;
          &lt;deny users="*"/&gt;
        &lt;/authorization&gt;
      &lt;/system.web&gt;
    &lt;/location&gt;
  </code></pre>
  
  <p>Now in the code I have the following code.</p>
  
  <pre><code>protected void UpdateUsers()
  {
      System.Configuration.Configuration config = (Configuration)WebConfigurationManager.OpenWebConfiguration("~");
      ConfigurationLocationCollection section = config.Locations;
  
      foreach (ConfigurationLocation location in section)
      {
          if(location.Path == "Admin")
          {
              AuthorizationSection admin_section = (AuthorizationSection)config.GetSection("system.web/authorization");
  
              AuthorizationRule thisAuth = new AuthorizationRule(AuthorizationRuleAction.Allow) ;
                  thisAuth.Users.Add("domain\\username");
  
               admin_section.Rules.Add(thisAuth);
               admin_section.CurrentConfiguration.Save();
          }
  
      }
  }
  </code></pre>
  
  <p>The above code is adding the section on the system.web and not on the admin location.</p>