I have created an aspx page where I can type a name into a texbox and it will add a line under the 'autorization' section in my web.config file (E.g. 'allow users="peter" '), the problem is the line is always added below 'deny users="" ' which automatically stops any user added below that line from logging in. Is there a way to find the 'deny users="" ' line via my C# code and insert the 'allow users' line above?
Thanks
Default.aspx.cs (Add user code)
protected void btnWrite_Click(object sender, EventArgs e)
{
System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
AuthorizationSection authorization = (AuthorizationSection)configuration.GetSection("system.web/authorization");
AuthorizationRule accessRule = new AuthorizationRule(AuthorizationRuleAction.Allow);
accessRule.Users.Add(txtAddUser.Text);
authorization.Rules.Add(accessRule);
configuration.Save(ConfigurationSaveMode.Minimal);
}
Web.config (authorization section)
<authentication mode="Forms">
<forms name=".ASPNET" loginUrl="login.aspx" defaultUrl="Default/default.aspx" />
</authentication>
<authorization>
<allow users="john" />
<deny users="*" />
<allow users="peter" />
<allow users="david" />
</authorization>