Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been on this project for quite a month now. the application is design based on users and role framework. I want to always check if the user logging in is an administrator, if true (the user is administrator) an admin menu will be displayed else the visible property of the menu is false. I implemented this check on every page in the application, Is there any way to write the code once and call it on page_Load() event of every other ASP.NET Page in the application. Here is the code?
C#
private void CheckIfUserIsAdmin()
    {
        if (Request.IsAuthenticated)
        {
            try
            {
                string username = User.Identity.Name;
                if (username == "admin" && User.IsInRole("Administrator"))
                {
                    Panel PanelAdmin = (Panel)Master.FindControl("PanelAdminMenu");
                    PanelAdmin.Visible = true;
                }
            }
            catch (NullReferenceException nullex)
            {
                nullex.Message.ToString();
            }
        }
    }
Posted
Updated 25-Aug-11 2:01am
v5
Comments
Prerak Patel 25-Aug-11 4:51am    
Use code block for code segments. Don't write in ALL CAPITAL.

Yes.

0) Create a base class that inherits from the System.Web.UI.Page class

1) Put your code in the new base class

2) Inherit the new base class (instead of Web.UI.Page) in all of your pages.
 
Share this answer
 
v2
Comments
ahmedfaruk88 25-Aug-11 8:27am    
What if I want to call this method from the Base Class. Let say I have a .cs file with a class called MyClass. Another .aspx file that inherit from Myclass. Check this code:


public partial class MyClass : System.Web.UI.Page
{
void CheckIfUserIsAdmin()
{
if (Request.IsAuthenticated)
{
try
{
string username = User.Identity.Name;
if (username == "admin" && User.IsInRole("Administrator"))
{
Panel PanelAdmin = (Panel)Master.FindControl("PanelAdminMenu");
PanelAdmin.Visible = true;
}
}
catch (NullReferenceException nullex)
{
nullex.Message.ToString();
}
}

else
{
Response.Redirect("UnathorizeErrorPage.aspx");
}
}
}

Then the .aspx page will look like this:
public partial class LeaveMessage : MyClass
{

}

how do I call the CheckIfUserIsAdmin() Method Declared in the Class File from the .aspx file

Thnanks
[no name] 25-Aug-11 9:57am    
You call it the same why you call any other method in a base class, the same way you are calling Response.Redirect.

Perhaps you need to review Object Oriented principles before proceeding.
Of course there is a way to write code and have it usable by multiple class, its called inheritance. Did you write the Request or User properties in your page, or each page you use? Of course not, your page is derived from System.Web.UI.Page.

Create a class that derives from System.Web.UI.Page, place this code in it and derive your existing pages from this class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900