简体   繁体   中英

asp.net c# user login access and authorization and menu

I am working on web application and I have 3 types of users. As we know different users have different access rights. Suppose there are three users A,B,C.

And there are total of 5 menus, example menu1,menu2.menu3,menu4,menu5.

I have to set access rights like

User A can access menu1,menu2.menu3,menu4,menu5.

User B can access menu2.menu3.

User C can access menu3,menu4,menu5.

In this I may add one more user having different access rights.

My question is how I design database and what logic I should apply to get this result.

I have one solution but I am trying it through the database. That solution is to keep different master pages for different users but that's not a good solution.

From login identify users access level. Inside master page have different regions for user levels

Reffer this -

how to make masterPage contents visible based on role? [a similar question]

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.loginview.rolegroups%28v=vs.110%29.aspx [A good guide]

-Happy Coding-

There are so many ways to do this; here is an example of what you can do:

  • Put all menus in one masterpage
  • On Session_Start get his access level from the database and store it in session variable
  • On masterpage Page_Load you can show and hide the menus based on the access level

I am assuming that you are only having Five menu's. You can do something like this.

As per My knowledge first you need to insert the information in to the database for your access control. So you can do some thing like this to insert values into the database for access control.

User        Menu-1        Menu-2      Menu-3     Menu-4    Menu-5

A             1             1           1          1         1

B             0             1           1          0         0                 

C             0             0           1          1         1

you can Use Repeater or Grdiview to display UI like above.

0 & 1 are nothing but the checkboxes .

0- Means checkbox is not checked & user cannot access that menu
1- Means checkbox is checked & user can access that menu

Table Structure

Create Table User_Details
(
  UserId int identity(1,1)
  UserName varchar(100),
  menu1 bit,
  menu2 bit,
  menu3 bit,
  menu4 bit,
  menu5 bit
)

This was about the inserting information into the database regarding your database.

Now for Access Control you can Do :

I am assuming that you have used User control for your menu.

If your user is logged in then you will have ID of the user and In all the page of your menu (User Control) you need to check that is your user has access to that particular Menu

IF YES : then You can make your panel of the menu visible for that user

IF NO : then You have to your panel of the menu invisible for that user

For object oriented approach and efficiency when it comes to data manipulation.
Create 2 tables: and
t_Types have columns like roles will be your access type, (ex. admin, normal user) 将是您的访问类型,(例如admin,普通用户)
t_Users have columns like where TypeID is a foreign key for you to assign what kind of user does the user belong. 类的列其中TypeID是外键,供您分配用户所属的用户类型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM