JianYA
JianYA

Reputation: 3024

Checking logged in user role in View in ASP.NET Core 2.2

A user in my web application may have several roles coming from different applications. Those roles are stored in the HttpContext in an array that is Serialized called Roles.

enter image description here

Currently for my controller I implement a custom filter that deserializes the array and reads the items within like this

public void OnActionExecuting(ActionExecutingContext context)
{
            string[] applications = ListOfApplications.Split(",");
            string[] roles = ListOfRoles.Split(",");
            var userRoles = context.HttpContext.User.Claims.Where(c => c.Type == "Roles").Select(c => c.Value).ToList();
            var matches = 0;
            foreach (var item in userRoles)
            {
                var currentItem = JsonConvert.DeserializeObject<UserRoleDetailsViewModel>(item);
                UserRoleDetailsViewModel urdvm = new UserRoleDetailsViewModel
                {
                    Id = currentItem.Id,
                    Name = currentItem.Name,
                    ApplicationId = currentItem.ApplicationId,
                    ApplicationName = currentItem.ApplicationName
                };
                for (var i = 0; i < applications.Length; i++)
                {
                    if(applications[i] == ApplicationGlobals.All && roles[i] == RoleGlobals.All)
                    {
                        matches++;
                    }
                    if(applications[i]== ApplicationGlobals.All && roles[i] == urdvm.Name)
                    {
                        matches++;
                    }
                    if(applications[i] == urdvm.ApplicationName && roles[i] == urdvm.Name)
                    {
                        matches++;
                    }
                }
            }
            if (matches == 0)
            {
                context.Result = new RedirectToActionResult("Index", "Home", null);
            }
}

And I call it on top of my controller like this:

[Authorize]
[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object[] {
        ApplicationGlobals.app1+","+ApplicationGlobals.app1
        ,RoleGlobals.SystemAdministrator+","+RoleGlobals.User
})]

However, this only works until the controller. How can I extend this to work in a view so that I am able to segment off areas in the navigation bar for example based on the users role? Is this possible?

Upvotes: 1

Views: 2689

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32069

However, this only works until the controller. How can I extend this to work in a view so that I am able to segment off areas in the navigation bar for example based on the users role? Is this possible?

Yes! possible. Following is the way of doing so in ASP.NET Core identity.

@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager // ApplicatonUser is the class that inherited IndentityUser
@inject UserManager<ApplicationUser> UserManager

<ul class="navbar-nav">
    @if (SignInManager.IsSignedIn(User))
    {
       // Here is navbar items for authenticated (logged in) user

       If(User.IsInRole("Admin");)
       {
           // Here is navbar items for only user with `Admin` Role
       }
    }
    else
    {
        // Here is navbar items for user if not logged in
    }
</ul>

Upvotes: 2

Related Questions