Asp.net user roles and permissions display different content
Asp tutorial. net user roles and permissions display different content
Using System;
Using System. Data;
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 Role;
Public partial class _ Default: System. Web. UI. Page
{
String strUserRole = string. Empty;
Protected void Page_Init (object sender, EventArgs e)
{
}
Protected void Page_Load (object sender, EventArgs e)
{
Menu1.DataSource = GetDataSource (strUserRole, Server. MapPath ("~ "));
Menu1.DataBind ();
}
XmlDataSource GetDataSource (string UserRole, string ServerPath)
{
XmlDataSource objData = new XmlDataSource ();
ObjData. XPath = "siteMap/siteMapNode ";
Switch (UserRole)
{
Case "Administrator ":
ObjData. DataFile = ServerPath + @ "/App_Data/Administrator. sitemap ";
Break;
Case "User ":
ObjData. DataFile = ServerPath + @ "/App_Data/User. sitemap ";
Break;
Default:
ObjData. DataFile = ServerPath + @ "/App_Data/Default. sitemap ";
Break;
}
ObjData. DataBind ();
Return objData;
}
Protected void btnAdmin_Click (object sender, EventArgs e)
{
StrUserRole = "Administrator ";
Menu1.DataSource = GetDataSource (strUserRole, Server. MapPath ("~ "));
Menu1.DataBind ();
}
Protected void btnUser_Click (object sender, EventArgs e)
{
StrUserRole = "User ";
Menu1.DataSource = GetDataSource (strUserRole, Server. MapPath ("~ "));
Menu1.DataBind ();
}
}
<? Xml version = "1.0" encoding = "UTF-8"?>
<SiteMap>
<SiteMapNode url = "~ /Home. aspx "title =" Home "description =" HomePage "/>
<SiteMapNode url = "~ /CreateUser. aspx "title =" CreateUser "description =" CreateUser "/>
<SiteMapNode url = "" title = "Products" description = "Products">
<SiteMapNode url = "~ /Software. aspx "title =" Software "description =" Software "/>
<SiteMapNode url = "Hardware. aspx" title = "Hardware" description = "Hardware"/>
</SiteMapNode>
</SiteMap>