atobrandon atobrandon - 1 year ago 57
C# Question

Using the Authorize attribute with Azure Active Directory

I'm trying to use [Authorize(Roles="test")] and/or call User.IsInRole("test") on a testing app and it's not working for me. In the app configuration in the Azure Management Portal I've enabled the SecurityGroup claim in the app manifest and also set the delegated permissions to allow for "Read all groups" and "Read directory data". After doing this I'm able to see the SID for the test group in (ClaimsPrincipal.Current.Identity as ClaimsIdentity).FindAll("groups"), but the Authorize attribute and call to IsInRole are not working. Is anything else necessary to allow this to work?

Answer Source

Azure AD does not send groups in the Role claim, and it does not send the name of groups (in a multitenant system, that would not mean much: "admin" in contoso might have a completely different semantic from "admin" in fabrikam). Azure AD has a new construct which represents actual roles, as opposed to group. If you want to check access against the name of a role, see for a sample using application roles. If you want to check authorization against a group instead, you need to assign the group claim type as RoleClaimType (in you can see how it's done for app roles) so that ASP.NET will know it's the claim to verify when Authorize and IsInRole come into play. Also, given that you don;t get the group's name you need to perform the check against the group's objectId. If you want to use groups AND check against group names, it gets complicated. You need to call the graph API to translate the group's ObjectId into its name and augment the incoming claims collection accordingly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download