November Happy Hour will be moved to Thursday December 5th.

Get User Roles in WebApi

Vote:
 

Hi I am testing WebApi calls and I Wanted to proble users and their roles.

I can get all roles, and user data but how can I check what roles a user has?

This is a plugin so I can not know what type of role providers a consuming project could have. COuld be Default ones, could be OWIN. Could be something different.

[HttpGet]
public IHttpActionResult GetCurrentUserData(string userName)
{
  var virtualRoleRepository = (RolesRepository ?? ServiceLocator.Current.GetInstance<IVirtualRoleRepository>()).GetAllRoles();
  
  var user = EPiServerProfile.Get(userName);
  
  //var userRoles = ?????
  
  if (user == null)
    
    return NotFound();
  
  return Ok(user);
}
#217449
Edited, Feb 24, 2020 12:32
Vote:
 

You can use UIRoleProvider.GetRolesForUser(userName)

the drawback is UIRoleProvider is in EPiServer.Shell.Security, i.e. EPiServer UI package 

#217450
Feb 24, 2020 12:40
Vote:
 

Thank you! I dont know if the namespace will be a problem, i would assume not and hope it will not :)

#217451
Feb 24, 2020 12:45
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.