I am trying to get the current user's roles in Orchard 1.9.2 from a view.
I tried this snippet of code but it always returns 0 roles:
var roles = ((ContentItem)WorkContext.CurrentUser.ContentItem).As<UserRolesPart>().Roles;
source: https://www.cloudconstruct.com/blog/retrieving-role-information-in-your-orchard-view-templates
Any ideas?
Perhaps the using statement is missing?
Try the following code:
edit 1
It seems like the user is not authenticated. The line of code posted above creates the following calls:
Orchard.Environment.WorkContextImplementation.GetState("CurrentUser")Orchard.Environment.WorkContextImplementation.FindResolverForState("CurrentUser")IWorkContextStateProvider, in this list there should be the following implementation:Orchard.Security.CurrentUserWorkContextOrchard.Security.CurrentUserWorkContext.Get("CurrentUser")IAuthenticationService.GetAuthenticatedUser()-> which returns the user when authenticatedSo it seems that any of these steps returns without the user. I suggest you create a breakpoint in
Orchard.Environment.WorkContextImplementation.GetState()to find out where the issue is.