<location path="page1"> <system.web> <authorization> <allow users="role1, role2, role 3, ..." /> <deny users="*" /> </authorization> </system.web> </location>
private void UpdateLocation(AuthorizationRule newRule, string location, string role) { string path = Server.MapPath("~/Web.Config"); XmlDocument xDoc = new XmlDocument(); xDoc.Load(path); XmlNodeList list = xDoc.DocumentElement.SelectNodes("location"); if (list.Count > 0) { XmlNode node = list[0]; XmlAttribute attribute = node.Attributes["path"]; attribute.Value = this.txtLocation.Text.Trim(); node.Attributes.Append(attribute); xDoc.Save(path); } else {//create location element and the path attribute with it's value set to the //selected page XmlElement newLocationelement = xDoc.CreateElement("location"); XmlAttribute newLocationAttrib = xDoc.CreateAttribute("path"); newLocationAttrib.Value = location; newLocationelement.Attributes.Append(newLocationAttrib); XmlElement newSystemWebelement = xDoc.CreateElement("system.web"); XmlElement newAuthorizationelement = xDoc.CreateElement("authorization"); //create the allow element XmlElement newAllowelement = xDoc.CreateElement("allow"); XmlAttribute newAllowAttrib = xDoc.CreateAttribute("users"); newRule.Roles.Add(role).ToString(); string listofRoles = ""; foreach (var item in newRule.Roles) { listofRoles = item.ToString(); } newAllowAttrib.Value = listofRoles; newAllowelement.Attributes.Append(newAllowAttrib); //create the deny element XmlElement newDenyelement = xDoc.CreateElement("deny"); XmlAttribute newUsersAttrib = xDoc.CreateAttribute("users"); newUsersAttrib.Value = "*"; newDenyelement.Attributes.Append(newUsersAttrib); newAuthorizationelement.AppendChild(newAllowelement); newAuthorizationelement.AppendChild(newDenyelement); newLocationelement.AppendChild(newSystemWebelement); newSystemWebelement.AppendChild(newAuthorizationelement); xDoc.DocumentElement.AppendChild(newLocationelement); xDoc.PreserveWhitespace = true; //write to web.config file using xml writer XmlTextWriter xwriter = new XmlTextWriter(path, null); xDoc.WriteTo(xwriter); xwriter.Close(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)