ASP.NET MVC 5 Customise Bootstrap navbar based on User Role

前端 未结 3 1930
耶瑟儿~
耶瑟儿~ 2021-02-19 20:44

I\'m using the ASP.NET MVC 5 built in authentication methods. I would like to show and hide links (in the menu navbar) based on the role the user is in.

Has anyone ache

3条回答
  •  醉梦人生
    2021-02-19 21:48

    You can use MvcSiteMap for this. It has a feature called SecurityTrimming which uses the [Authorize] attribute on your action methods to decide whether or not to display the menu item.

    I know it's frowned upon to post a links in answers but I found this blog post very useful.

    In addition to the role-based menu visibility, I added custom attributes to the MvcSiteMapNodes to determine visibility of links that were accessible to users but I didn't want shown in the menu (e.g. Edit pages), and I also added icon attributes which allowed me to use the bootstrap menu icons e.g:

    
    

    I went a bit off-topic there, but I just wanted to highlight how flexible MvcSiteMap is.

提交回复
热议问题