Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how if when everytime i click update .. i can role update automatically .. like below :
XML
<location path="page1">
    <system.web>
      <authorization>
        <allow users="role1, role2, role 3, ..." />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>



so far below my code for updating , but it's yet for updating role
C#
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();

       }
   }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900