Reputation: 1063
I'm trying a basic customization of my _layourpartial file by returning a list item for only users within admin role
<ul class="nav navbar-nav navbar-right">
@if (User.IsInRole("admin"))
{
<li>@Html.ActionLink("Dashboard", "Dashboard", "Home")</li>
}
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
</ul>
The problem is that when i delete a user from admin role , User.IsInRole still returns true.
I tried to delete cookies, logout/login, restart iis express ,...
Nothing works!
Upvotes: 0
Views: 1233
Reputation: 14741
In Identity current users roles store as claims and User.IsInRole()
checks applied claims not actual roles. To remove a role from current users claims do following:
var identity = (User.Identity as ClaimsIdentity);
var adminClaim = identity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role && c.Value == "admin");
if(adminClaim!=null)
identity.RemoveClaim(adminClaim);
Or you could check actual roles of current user instead of assigned claims by following code:
HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>()
.IsInRole(User.Identity.GetUserId(), "admin");
Upvotes: 2