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" })
Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top