Hi gokuldas,
Refering the below link i have created an example. Now please take its reference and correct your code.
First you need to allow all the user to access the pages. For this you need to modify SiteMap.
Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="Home" description="" roles ="*">
<siteMapNode url="~/Home.aspx" title="Home" description="Home Page" roles="*" />
<siteMapNode url="javascript:;" title="Admin" description="Admin Page" roles ="*">
<siteMapNode url ="~/Admin/Users.aspx" title="Users" description="Users Page"></siteMapNode>
<siteMapNode url ="~/Admin/Reports.aspx" title="Reports" description="Reports Page"></siteMapNode>
</siteMapNode>
<siteMapNode url="~/Contact.aspx" title="Contact" description="Contact Us Page" roles="*" />
</siteMapNode>
</siteMap>
HTML
Site1.master
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
.main_menu
{
width: 100px;
background-color: #fff;
border: 1px solid #ccc !important;
color: #000;
text-align: center;
height: 30px;
line-height: 30px;
margin-right: 5px;
}
.level_menu
{
width: 110px;
background-color: #fff;
color: #333;
border: 1px solid #ccc !important;
text-align: center;
height: 30px;
line-height: 30px;
margin-top: 5px;
}
.selected
{
background-color: #9F9F9F;
color: #fff;
}
input[type=text], input[type=password]
{
width: 200px;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LoginView ID="LoginView" runat="server">
<LoggedInTemplate>
<div align="right">
Welcome
<asp:LoginName ID="LoginName1" runat="server" Font-Bold="true" />
<br />
<br />
<asp:Label ID="lblLastLoginDate" runat="server" />
<asp:LoginStatus ID="LoginStatus1" runat="server" />
</div>
<hr />
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false"
SiteMapProvider="SiteMap" />
<asp:Menu ID="Menu" runat="server" DataSourceID="SiteMapDataSource1" Orientation="Horizontal"
OnMenuItemDataBound="OnMenuItemDataBound" OnUnload="OnMenuItemUnload">
<LevelMenuItemStyles>
<asp:MenuItemStyle CssClass="main_menu" />
<asp:MenuItemStyle CssClass="level_menu" />
</LevelMenuItemStyles>
</asp:Menu>
</LoggedInTemplate>
</asp:LoginView>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Web.Security;
using System.Security.Principal;
Code
Site1.master.cs
protected void OnMenuItemDataBound(object sender, MenuEventArgs e)
{
if (SiteMap.CurrentNode != null)
{
if (e.Item.Text == SiteMap.CurrentNode.Title)
{
if (e.Item.Parent != null)
{
e.Item.Parent.Selected = true;
}
else
{
e.Item.Selected = true;
}
}
}
}
protected void OnMenuItemUnload(object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string userData = ticket.UserData;
string[] roles = userData.Split(',');
Menu Menu1 = (this.LoginView as LoginView).FindControl("Menu") as Menu;
if (roles[0] == "User")
{
MenuItemCollection menuItemCollection = Menu1.Items;
for (int i = 0; i < menuItemCollection.Count; i++)
{
MenuItem menuItem = menuItemCollection[i];
if (menuItem.Text == "Admin")
{
menuItem.Enabled = false;
}
}
}
}
}
}
}
Site1.master.vb
Protected Sub OnMenuItemDataBound(sender As Object, e As MenuEventArgs)
If SiteMap.CurrentNode IsNot Nothing Then
If e.Item.Text = SiteMap.CurrentNode.Title Then
If e.Item.Parent IsNot Nothing Then
e.Item.Parent.Selected = True
Else
e.Item.Selected = True
End If
End If
End If
End Sub
Protected Sub OnMenuItemUnload(ByVal sender As Object, ByVal e As EventArgs)
If (Not (HttpContext.Current.User) Is Nothing) Then
If HttpContext.Current.User.Identity.IsAuthenticated Then
If (TypeOf HttpContext.Current.User.Identity Is FormsIdentity) Then
Dim id As FormsIdentity = CType(HttpContext.Current.User.Identity, FormsIdentity)
Dim ticket As FormsAuthenticationTicket = id.Ticket
Dim userData As String = ticket.UserData
Dim roles() As String = userData.Split(Microsoft.VisualBasic.ChrW(44))
Dim Menu1 As Menu = CType(CType(Me.LoginView, LoginView).FindControl("Menu"), Menu)
If (roles(0) = "User") Then
Dim menuItemCollection As MenuItemCollection = Menu1.Items
Dim i As Integer = 0
Do While (i < menuItemCollection.Count)
Dim menuItem As MenuItem = menuItemCollection(i)
If (menuItem.Text = "Admin") Then
menuItem.Enabled = False
End If
i = (i + 1)
Loop
End If
End If
End If
End If
End Sub
Screenshot