Hello,
I have designed one web application with one master page and login web application. On master page, I have taken navigation menu with three items. On login form, I have taken two text boxes for username,password and one button for Login. Now I want to remove items from navigation menu, according to user authorization level.
For eg, User login as Senior engineer then i want to give access only two items from navigation menu. So how can I achieve this.
For reference, attached herewith application code.
Page.Master
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Page.master.cs" Inherits="CRM_CSharp_1.Page" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title></title><link rel="Stylesheet" type="text/css" href="~/Styles/General.css" /><link rel="Stylesheet" type="text/css" href="~/Scripts/ConfirmDelete.js" /><style type="text/css"> .style1 { color: #FFFFFF; font-size: xx-large; } #form1 { margin-bottom: 19px; }</style></head><body><form id="form1" runat="server"><div id="header" class="style1" style="background-color: #EEEEEE; font-family: 'Microsoft Sans Serif'; color: #000000; font-size: medium;"><asp:Image ID="IMGlogo" runat="server" ImageAlign="Right" ImageUrl="~/Image/logo.bmp" style="top: 18px; position: absolute; float: right; height: 67px; width: 192px; left: 28px; right: 767px;" /> <asp:Label ID="Label1" runat="server" Font-Bold="False" Font-Size="X-Large" Font-Underline="False" Text="Customer Relationship Management"></asp:Label> <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false"><AnonymousTemplate> [ <a href="WebForm1.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]</AnonymousTemplate><LoggedInTemplate> Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>! [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]</LoggedInTemplate></asp:LoginView> <asp:Label ID="Label2" runat="server" style="font-size: small; color: #000000" Text="Current User Name : "></asp:Label> <asp:TextBox ID="TextBox1" runat="server" Width="150px" BackColor="#EEEEEE" BorderStyle="None" Font-Bold="True" ForeColor="Navy"></asp:TextBox> <asp:Label ID="Label3" runat="server" style="font-size: small; color: #000000" Text="Current User Role : "></asp:Label> <asp:TextBox ID="TextBox2" runat="server" Width="150px" BackColor="#EEEEEE" BorderStyle="None" ForeColor="Navy"></asp:TextBox> <br /> <br /> <br /> </div><div id="menuContainer" style="background-color: #EEEEEE"><asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Vertical" Font-Size="Small" onmenuitemclick="NavigationMenu_MenuItemClick"><Items><asp:MenuItem Text="User Administration" Value="User Administration" NavigateUrl="~/AddNewUser.aspx"><asp:MenuItem NavigateUrl="~/FilterEmpDetails_Report.aspx" Text="Employee Tracking Report" Value="Employee Tracking Report"></asp:MenuItem></asp:MenuItem><asp:MenuItem Text="Visit Information" Value="Visit Information" NavigateUrl="~/Default.aspx"><asp:MenuItem NavigateUrl="~/Default.aspx" Text="Add" Value="Add"></asp:MenuItem></asp:MenuItem><asp:MenuItem NavigateUrl="~/Cust_Feedback.aspx" Text="Customer Visit Information" Value="Customer Visit Information"></asp:MenuItem></Items></asp:Menu> <br /> <asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Size="Large" ForeColor="#0000CC" Height="40px" onclick="Button1_Click" Text="Logout" Width="191px" /><br /> </div><div id="mainContainer"><asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder></div> <div id="footer" class="style1" style="background-color: #EEEEEE; font-family: 'Microsoft Sans Serif'; font-size: small; height: 38px;"><br /> www.maestrotek-innovation.com</div> </form></body></html>
WebForm1.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Page.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CRM_CSharp_1.WebForm1" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" runat="server"><h2 style="font-family: 'Microsoft Sans Serif'; color: #000080; background-color: #EEEEEE; height: 30px;"> Log In </h2><p style="color: #FF0000; height: 21px;"> Please enter your username and password.<asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink> if you don't have an account. </p><asp:Panel ID="Panel1" runat="server" Height="159px" Width="456px" BackColor="#EEEEEE" BorderColor="Black" BorderStyle="Double"><br /> <asp:Label ID="Label2" runat="server" Font-Bold="False" Font-Size="Medium" Text="Username :-"></asp:Label> <asp:TextBox ID="UsernameTB1" runat="server" Width="222px"></asp:TextBox> <br /><br /> <asp:Label ID="Label3" runat="server" Font-Bold="False" Font-Size="Medium" Text="Password :-"></asp:Label> <asp:TextBox ID="PasswordTB2" runat="server" Width="222px" TextMode="Password"></asp:TextBox><br /> <asp:ImageButton ID="ImageButton1" runat="server" Height="71px" ImageUrl="~/Image/icon_login.png" onclick="ImageButton1_Click" Width="107px" ImageAlign="Middle" /> </asp:Panel> </asp:Content>
WebForm1.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace CRM_CSharp_1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public void Remove_tabs() { System.Web.UI.WebControls.Menu mainMenu = (System.Web.UI.WebControls.Menu)Master.FindControl("NavigationMenu"); if (mainMenu.Items[0].Value == "User Administration" && mainMenu.Items[1].Value == "Visit Information") { mainMenu.Items.Remove(mainMenu.Items[0]); mainMenu.Items.Remove(mainMenu.Items[1]); } } //Login Button Click Event protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { SqlConnection con = new SqlConnection(); con.ConnectionString = "Server=tcp:trackingdb19.database.windows.net,1433;Database=TrackingDB;User ID=user@trackingdb19;Password=Maestro@123;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"; con.Open(); //sql querry { string sqlselect = "select * from Employee_Master where EmpUserName=@username and EmpPassword=@password"; //sql command to manipulate sql data table SqlCommand sqlcommand = new SqlCommand(sqlselect, con); string userName = UsernameTB1.Text as String; Session["New"] = userName; string pwd = PasswordTB2.Text as String; sqlcommand.Parameters.Add("@username", SqlDbType.Char).Value = userName; sqlcommand.Parameters.Add("@password", SqlDbType.Char).Value = pwd; //Cheking if the connection was open or not if (con.State == ConnectionState.Closed) con.Open(); //reading data from database table to check if the query is true or false SqlDataReader reader = sqlcommand.ExecuteReader(); //if the user exist if (reader.Read()== true) { //declaring variables to store data from the database string role = Convert.ToString(reader["EmpUserRole"]); string username = Convert.ToString(reader["EmpUsername"]); Session["New1"] = role; Session["TextBox2.text"] = username; //if the user is a general employee then some of the buttons will be disabled if (role == "General Employee") { System.Web.UI.WebControls.Menu mainMenu = (System.Web.UI.WebControls.Menu)Master.FindControl("NavigationMenu"); if (mainMenu.Items[0].Value == "User Administration" && mainMenu.Items[1].Value == "Visit Information" && mainMenu.Items[2].Value == "Customer Visit Information") { mainMenu.Items.Remove(mainMenu.Items[0]); mainMenu.Items.Remove(mainMenu.Items[1]); mainMenu.Items.Remove(mainMenu.Items[2]); } } else if (role == "Senior Engineer") { System.Web.UI.WebControls.Menu mainMenu = (System.Web.UI.WebControls.Menu)Master.FindControl("NavigationMenu"); if (mainMenu.Items[0].Value == "User Administration") { mainMenu.Items.Remove(mainMenu.Items[0]); } } else if (role == "Junior Engineer") { System.Web.UI.WebControls.Menu mainMenu = (System.Web.UI.WebControls.Menu)Master.FindControl("NavigationMenu"); if (mainMenu.Items[0].Value == "User Administration") { mainMenu.Items.Remove(mainMenu.Items[0]); } } else if (role == "Sales Engineer") { Remove_tabs(); } else if (role == "Administrator") { } //displaying data from the database on the home form TextBox str1 = Master.FindControl("TextBox1") as TextBox; str1.Text = username; TextBox str2 = Master.FindControl("TextBox2") as TextBox; str2.Text = role; } else { Response.Write("<script>alert('Username and password combination is wrong')</script>"); } reader.Close(); sqlcommand.Dispose(); if (con.State == ConnectionState.Open) con.Close(); //clearing user data UsernameTB1.Text = ""; //clearing Password PasswordTB2.Text = ""; } } } }
Please do needful.
Thanks & Regards
MTrush