You'll want to use the Role
class, but really what you should be doing is collecting this information together in the controller & passing it, probably in the ViewBag
. That way your view is abstracted from where you get the username & roles from.
How can I display an Identity User's roles?
-
18-10-2022 - |
Question
I want to adjust the _LoginPartial.cshtml to display:
Logged in as <username>(<rolename>) | Logoff
So I wish to add the first role name associated with the logged in user.
I can't access it via the user object, so how do I access this information?
This is the current code:
@Html.ActionLink("Logged in as " + User.Identity.GetUserName(), "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
Solution
OTHER TIPS
If you're using claims, then you can downcast the User to a ClaimsPrincipal and then filter the Claims collection on the Type == ClaimsTypes.Role.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow