I'm trying to add Roles authentication to an Action in a Controller in an ASP.NET MVC application. The code looks something like this:
[Authorize(Roles = "SomeRoleName")]
public ActionResult Index()
{
bool inRole = User.IsInRole("Admin");
If I remove the Authorize attribute and put a breakpoint on the last line in that code sample, is there a way that I can inspect the objects and find out what roles are available?
e.g. I call User.IsInRole("Admin) in the Immediate window and it will give me a true/false value. How can I access the collection of roles available?
Add this to your web.config under system.web:
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"/>
Then you can use:
string[] arr = Roles.GetRolesForUser(User.Identity.Name);
or:
string[] arr = Roles.GetRolesForUser();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With