如何根据用户角色禁用菜单项和子菜单 [英] How to disable Menuitems and submenus based on user roles

查看:131
本文介绍了如何根据用户角色禁用菜单项和子菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我已经要求启用或禁用asp.net中的菜单项和子菜单项我已经完成了但是当我粘贴了没有权限的内容的网址时,它也会填充该角色。所以请帮帮我

Hi guys iam having requirement of enabling or disabling the menu items and submenu items in asp.net i've done that but when iam pasting the url of something that is not having the permission it is also populating in that role. so please help me

推荐答案

尝试下面的代码:



考虑有两个角色Admin和User 。 Menu1是主菜单的ID,它包含3个子菜单AdminReports,MasterReport,UserReport。管理员可以看到AdminReport和MasterReport,但不能看到UserReport。用户角色可以看到UserReport而不是其他报告。

Try with below code:

Consider there are two roles Admin and User. Menu1 is ID of main menu and it contains 3 sub menus AdminReports, MasterReport, UserReport. Admin can see AdminReport and MasterReport but not UserReport. User role can see UserReport not other reports.
protected void Page_Load(object sender, EventArgs e)
{
	// Get user role from session
	string role = Session["role"];
	if (role == "User")
	{
        // Disable the sub menu item
		MenuItem menuItem = Menu1.FindItem("AdminReport");		
		menuItem.Enabled = false;
		
		// Remove the sub menu item
		MenuItem menuItem1 = Menu1.FindItem("MasterReport");
		Menu1.Items.Remove(menuItem1);	
	}
	else if(role == "Admin")
	{
		MenuItem menuItem = Menu1.FindItem("UserReport"); 
		// Disable the sub menuitem
		menuItem.Enabled = false;			
	}
}


好吧应该够简单。

您有自己的方法来检查是否用户是否可以访问它提供的用户角色?



为了示例,我们称之为UserHasRole。

UserHasRole返回一个布尔值。



所以在你的aspx页面中尝试使用codeBlocks。

alright should be simple enough.
You have your method for checking if the user does have access to the user role it is provided?

Lets call it UserHasRole for the sake of the example.
UserHasRole returning a boolean value.

so in your aspx page try using codeBlocks.
<![CDATA[<% if (UserHasRole(userrole,userID))

{%>]]>
     Declare menuItem Here
<![CDATA[<%}%>]]>







UserHasRole可能需要在尝试访问它的页面的代码隐藏中声明。这取决于你的asp.net appplication在设置中的方式。




UserHasRole might need to be declared in the codebehind of the page trying to access it. That is dependant on how your asp.net appplication in setup.


感谢Manas_Kumar,但我的问题在这里我是从数据库获得角色权限

所以根据必须显示该菜单项

例如

User1具有5,8,9,15菜单项访问权限,即菜单子项(报告),分析,添加,删除等,

用户2可以访问10,20,30个菜单项,即查看,打印,部署等

类似其他用户

所以,当用户1在他的地址栏中复制10个menuitem网址时,它显示该页面,我不想显示likethat

所以根据我必须保留的数据库用户可以看到菜单。



我忘记提示菜单也根据用户类型动态显示
Thanks Manas_Kumar,but my Problem Here is i am getting role permissions from the database
so according to that menu items has to be displayed
for example
User1 have menu item access of 5,8,9,15 i.e, menu sub items (reports),analysis,adding,deleting etc,
user 2 have access for 10,20,30 menu items ie view, print, deploy etc
similarly the other users
so when ever user1 copying the 10 menuitem url in his address bar it is showing that page, i dont want to display likethat
so according to the database i must keep the menus visible to the users.

and i forget to mention that the menu is also displaying dynamically according to the type of user


这篇关于如何根据用户角色禁用菜单项和子菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆