ASP.NET MVC 4 How do you serve different HTML based on Role?

后端 未结 3 530
滥情空心
滥情空心 2021-01-19 04:44

On top of allowing only certain Roles to access certain Controller or Action, I would like to serve a slightly different HTML based on the roles.

Admin can see Edit

3条回答
  •  无人及你
    2021-01-19 05:41

    For menus, consider using MVCSiteMapProvider, which, like other SiteMapProviders, can be configured to use "security trimming", i.e. to only display nodes that the current user is authorized to use. I.e. a site map can be used to generate a menu which will automatically respect the Authorize attributes on controllers and actions.

    For visibility of controls (edit button, delete button and the like), add boolean properties to your Model: CanEdit, CanDelete etc, and have your controller populate them based on the user's roles (User.IsInRole).

    Separation of concerns dictates that you shouldn't access Roles directly in the View.

提交回复
热议问题