This code will add Location and Update the Location Path.
If the Location Node is not available then code will create the Location and Node and add it in the Web Config file.
HTML
Role
<asp:TextBox ID="txtRole" runat="server" />
<br />
Location
<asp:TextBox ID="txtLocation" runat="server" />
<br />
<asp:Button ID="btnSave" Text="text" runat="server" OnClick="InsertUpdateAuthorization" />
Update Location path
<asp:Button ID="btnUpdateLocationPath" runat="server" Text="Update Location Path" OnClick="UpdateLocationPath" />
Namespaces
using System.Xml;
using System.Data.SqlClient;
using System.Net;
using System.Web.Configuration;
C#
protected void InsertUpdateAuthorization(object sender, EventArgs e)
{
AuthorizationRule newRule;
newRule = new AuthorizationRule(AuthorizationRuleAction.Allow);
AddRoleRule(newRule, txtLocation.Text.Trim(), txtRole.Text.Trim());
}
protected void UpdateLocationPath(object sender, EventArgs e)
{
AuthorizationRule newRule;
newRule = new AuthorizationRule(AuthorizationRuleAction.Allow);
this.UpdateLocation(newRule, txtLocation.Text.Trim(), txtRole.Text.Trim());
}
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();
}
}
private void AddRoleRule(AuthorizationRule newRule, string location, string role)
{
string path = Server.MapPath("~/Web.Config");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
//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();
}