We came up with the same process of using different XmlFiles (granted we came to so many user's etc that we moved to an sql database) but yes let me put together some code for you below.
XmlStructure
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode url="/menu/TSRPortal.aspx" title="Dashboard" image="~/Menu/images/icons/dashboard.png" description="Dash Board" />
<siteMapNode url="/TSRPortal/v4/HoursReview/" title="Personal Hours Review" image="~/Menu/images/icons/hours_icon.png" description="Personal Hours Review" />
<siteMapNode url="/TSRPortal/v4/PayrollReview/" title="Payroll Review" image="~/Menu/images/icons/payroll_icon.png" description="Payroll Review" />
<siteMapNode url="/tools/intranet/HRTicket_Tracker.aspx" title="HR Request" image="~/Menu/images/icons/intranet_icon.png" description="HR Request" />
<siteMapNode url="/tools/intranet/intranet.aspx" title="Intranet" image="~/Menu/images/icons/intranet_icon.png" description="Corp Intranet" />
<siteMapNode url="/menu/bug/" title="Report A Bug" image="~/Menu/images/icons/bug.png" description="Corp Intranet" />
</siteMap>
Security Check Class
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
/// <summary>
/// Summary description for SecurityCheck
/// </summary>
public class SecurityCheck
{
private string _role;
public string role { set { _role = value; } }
private string _url;
public string url { set { _url = value; } }
public void check()
{
if (_role == null)
return;
XmlDocument doc = new XmlDocument();
if (_role == "admin")
doc.Load("admin.xml");
else if (_role == "user")
doc.Load("user.xml");
else
HttpContext.Current.Response.Redirect("invalidCred.aspx");
bool authenticate = false;
foreach (XmlNode node in doc.ChildNodes[1])
{
authenticate = CheckChildNodes(node);
if(authenticate)
break;
}
doc = null;
if(!authenticate)
HttpContext.Current.Response.Redirect("invalidCred.aspx");
} private bool CheckChildNodes(XmlNode node)
{
bool auth = false;
if (node.HasChildNodes())
{
foreach (XmlNode child in node.ChildNodes)
{
if (child.HasChildNodes)
auth = CheckChildNodes(child);
if (child.Attributes["url"].ToString().ToLower() == _url)
auth = true;
if (auth)
break;
}
}
else
if (node.Attributes["url"].ToString().ToLower() == _url)
auth = true;
return auth;
}
The above will take your code behind values, load the appropriate url and loop through all the nodes inside that XML File. It will also loop through all child nodes
Code Behind
protected void Page_Load(object s,EventArgs e)
{
if(!Page.IsPostBack)
{
SecurityCheck objSc = new SecurityCheck();
objSc.role = "admin"; //user role here
objSc.url = Request.Path.ToString();
objSc.check();
}
} Thie above does quite a bit of work. The Code behind just intiates the check, if the check passes the page loads, if the check fails the user is redirected to an invalid credentials page