In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to display the function of menu items in ASP.NET MVC". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Problem scenario
After developers apply CLK.AspNet.Identity in the system, they can restrict which functions users can use according to the permissions of the users. When the user enters the permission function page through the browser, he will receive the HTTP 403 status code, which is used to inform the user that he does not have permission to enter the page.
From the point of view of providing a better user experience, the user clicks on the menu item and gets a response that does not have permission to enter the page. This kind of operation process, it is easy to make users impatient. In order to provide a better user experience, the system should display menu items according to the user's rights, and only show menu items that have permission to enter and use, in order to reduce the chance of user operation errors.
Solution
In systems that apply CLK.AspNet.Identity, the extension method of HasPermission can be used to provide the function of displaying menu items according to permissions. The method of use is as follows:
Add a new ProductController and ProductViews to the system, and according to the following example program, add Method and the corresponding permissions: ProductAddAccess, ProductRemoveAccess.
Public class ProductController: Controller {[RBACAuthorize (Permission = "ProductAddAccess")] public ActionResult Add () {ViewBag.Message = "Your product add page."; return View ();} [RBACAuthorize (Permission = "ProductRemoveAccess")] public ActionResult Remove () {ViewBag.Message = "Your product remove page."; return View ();}}
Edit Views\ Shared_Layout.cshtml, and use the HasPermission extension method to display menu items according to the user's permissions, as shown in the following example.
If (User.HasPermission ("ProductAddAccess")) {@ Html.ActionLink ("Product add", "Add", "Product", null, new {@ class = "btn btn-primary"})} @ if (User.HasPermission ("ProductRemoveAccess")) {@ Html.ActionLink ("Product remove", "Remove", "Product", null) New {@ class = "btn btn-primary"})}
After completing the above program development work, you also need to enter the system and set user rights. In the following example, we demonstrate the use of Permission management pages to expose permissions to Admin groups.
After logging in using the default management account (ID:admin@example.com, PW:admin), you can see the menu item Product add in the system menu because admin@example.com belongs to the Admin group and the Admin group has ProductAddAccess permission.
After changing the default guest account login (ID:guest@example.com, PW:guest), you can see that because guest@example.com belongs to the Guest group, but the Guest group does not have ProductAddAccess permission, the menu item Product add is not seen in the system menu.
This is the end of the content of "how to display the function of menu items in ASP.NET MVC". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.