简体   繁体   中英

Enable Specific Page according to the user

Using ASP.Net and C#

Webpage Menus are Admin, HR, IT

I am using Login Page in my Website.

Condition

If the website is login by Admin User, Admin Menu only enable
If the website is login by HR User, HR menu only enable
....

How to make a code for the above condition.

Need Code Help

I am assuming you are using the builtin roleManager here and membership.

If you are using a menu control I believe the menu will honor location permissions if the target links are setup appropriately. So if you add to your web.config something like this (haven't checked exact markup, but hope you get the idea):

<location path="admin/admin.aspx">
  <system.web>
    <authorization>
      <allow roles="Admin" />
    </authorization>
  </system.web>
</location>
<location path="hr/hr.aspx">
  <system.web>
    <authorization>
      <allow roles="HR" />
    </authorization>
  </system.web>
</location>

If you are building a custom menu something as simple as

<% if (HttpContext.Current.User.IsInRole("Admin"){ %> <li>Admin Menu</li> <% } %> 

around your menu items should do the trick

Any control with runat="server" has a Visible attribute that can be bound to using,

Visible='<%# IsAdmin %>'

In the code IsAdmin is a property you create that returns a bool. An invisible item won't be rendered. Set the visible property for each of your menu's to IsAdmin, IsHR and IsIT and only those menu's will get viewed when their property return's true.

By the way, it's a habit to use single quotes around the bind because sometimes you want to use a double quote within the bind statement. In this case double quotes would work just fine as well.

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