Last active
October 6, 2020 11:52
-
-
Save depuits/8ae37a2db0a44abea6bfc30c72349f27 to your computer and use it in GitHub Desktop.
PrincipalExtensions for checking user roles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Security.Principal; | |
public static class PrincipalExtensions | |
{ | |
public static bool IsInAllRoles (this IPrincipal principal, params string[] roles) | |
{ | |
return roles.All (r1 => r1.Split (',').All (r2 => principal.IsInRole (r2.Trim()))); | |
} | |
public static bool IsInAnyRoles (this IPrincipal principal, params string[] roles) | |
{ | |
return roles.Any (r1 => r1.Split (',').Any (r2 => principal.IsInRole (r2.Trim()))); | |
} | |
} |
Not working on razor view
Not working on razor view
What exactly isn't working? Do you get any errors?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you change "r2 => principal.IsInRole (r2)" to "r2 => principal.IsInRole (r2.Trim())", it will work for "admin, user, manager" ...