如何根据用户角色禁用菜单项和子菜单

编程入门 行业动态 更新时间:2024-10-09 18:17:23
本文介绍了如何根据用户角色禁用菜单项和子菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

Hi guys iam having requirement of enabling or disabling the menu items and submenu items in asp 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 appplication在设置中的方式。

UserHasRole might need to be declared in the codebehind of the page trying to access it. That is dependant on how your asp 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

更多推荐

如何根据用户角色禁用菜单项和子菜单

本文发布于:2023-11-28 10:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜单项   菜单   角色   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!