Asp.net member can view user object access for roles

My controllers have this code:

    private void PopulateAdminStatus()
    {
        if (User.IsInRole("Administrator"))
        {
            ViewData["isAdmin"] = true;
        }
        else
        {
            ViewData["isAdmin"] = false;
        }
    }

The only reason I need to do this is to check the status of my view in admin status (since it shows different things in the view)

Is there any cleaner way to access the User objects without viewing the ViewData?

+3
source share
1 answer

you can do this in your view / partial view

 <% if(Page.User.IsInRole("Administrator")){%>

As RobCon says: β€œIf theres IF, make a helper” so that you can transfer your / admin check role to the helper and call the helper in your view every time you need a check.

+3

Source: https://habr.com/ru/post/1714622/


All Articles