[Authorize(Roles = "HRManager,Finance")]
public class SalaryController : Controller
{
public IActionResult Payslip() =>
Content("HRManager || Finance");
}
This code is from Microsoft and it knows what Roles the person used to access this method. How do I capture that list and set it to a list object? Is it possible? I feel like this should be simple but cant find any code examples for what I am looking for.
So I have tried several things that don't work but I feel like they should.
var claimsIdentity = (ClaimsIdentity) HttpContext.User.Identity;
var userIdentity = (ClaimsIdentity) User.Identity;
var claims = userIdentity.Claims;
var roleClaimType = userIdentity.RoleClaimType;
var identity = WindowsIdentity.GetCurrent().User;
// or...
var roles = claims.Where(c => c.Type == roleClaimType).ToList();
This last line seemed like it would work but again fails to provide human readable roles it just has in them some GroupIDs that appear to be binary.
Ultimately this last bit works but I am still not able to get a list of roles from this. Which is what I wanted originally.
var IsInRole = HttpContext.User.IsInRole("Admin");
If someone could provide a code sample that can turn this into a list of roles. That is the results I am looking for. The code should be simple and easy nothing more than a line or two to accomplish what I am looking for.