1

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" })
Fred Fickleberry III
  • 2,439
  • 4
  • 34
  • 50

2 Answers2

1

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.

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
  • If Frankie is putting the role name on the _LoginPartial.cshtml then he would have to get the role name in every single controller and place it in the ViewBag. Would it be best to make a base controller or is there a better method? – Sean Newcome Mar 03 '14 at 19:11
  • Base controller would probably be the best approach – Simon Halsey Mar 03 '14 at 21:54
  • Another approach instead of moving the aforementioned LoginPartial's logic to a base controller and shoving into ViewBag for every request, is to isolate and have this as a Child Action (in its own controller if you want) and call it via `Html.RenderAction` instead of `Html.Partial`... For extra credit you could even create a specific ViewModel for this and avoid ViewBag altogether. – Funka Jun 11 '14 at 23:19
0

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.

Brock Allen
  • 7,385
  • 19
  • 24