Show/Hide nav menu based on claims Identity ASP.NET CORE (not role base)

849 Views Asked by At

I've successfully implemented role and claim (policy) based authorisation in my app. I'm using ASP.NET CORE 7 MVC. The top menu navigation is hidden base on the login user's role something like this:

 @if (User.IsInRole("Admin"))
 {      
   <li class="nav-item"><a class="nav-link text-dark" asp-action="" asp- controller="">Audit</a>  
   </li> 
 }

so the above will only show if the login user has a role of Admin (doesn't matter the policy assigned). My question is I want to show/hide the nav like based on the claim/policy assigned to other users (other than an Admin, say I want to select a specific user to access the audit page)

I thought I could do something like this (but it's not working)

@if(User.HasClaim(c => c.Type == "ViewAuditPage" && c.Value == "True"))
{      
   <li class="nav-item"><a class="nav-link text-dark" asp-action="" asp- controller="">Audit</a>  
   </li> 
 }

Any idea how to show/hide nav menu base on claim/policy Authorisation?

0

There are 0 best solutions below