1

I've set up sign-in for multi-tenant Azure Active Directory using custom policies in Azure Active Directory B2C, so administrators of the Azure ADs can manage their own users. The sign-in works and I now want to support app roles.

I've defined app roles in the application manifest of the Azure AD B2C application and the roles are selectable in the Azure ADs. So far, so good, but the roles claim isn't included in the obtained token when signing in. I found that the roles claim isn't included by default in tokens issued by Azure AD B2C, but is it somehow possible to include the roles?

The roles are defined in the application manifest:

Application Manifest

The roles are selectable in the Azure ADs:

Users and groups Add assignment

Martin4ndersen
  • 2,806
  • 1
  • 23
  • 32

2 Answers2

0

Please check if below references can be worked around:

  1. Please try to include assign the user to the same app roles from azure ad tenant by assigning users/groups to the created app roles as you did.Then refresh the portal and app and Then try to get token from b2c endpoint.

  2. Please try to check the use of custom claims in azure ad B2C in which the consumer can select required role during the signup process which is returned in the token as well. reference: https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-rest-api-step-custom for more details.

  3. Try to create an extension attribute with some name extension_role . read the attribute on sign in/up:

    <TechnicalProfile Id="AAD-UserReadUsingObjectId">
      <Metadata>
        <Item Key="Operation">Read</Item>
        <Item Key="RaiseErrorIfClaimsPrincipalDoesNotExist">true</Item>
      </Metadata>
      <IncludeInSso>false</IncludeInSso>
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="objectId" Required="true" />
      </InputClaims>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="extension_role" />
      </OutputClaims>
      <IncludeTechnicalProfile ReferenceId="AAD-Common" />
    </TechnicalProfile>
    

reference: custom-roles-on-azure-ad-b2c|SO

  1. Other wise you can try to manually configure app to call microsoft graph api and get the role in token:

reference

     public async Task<string> GetUserRoleByObjectId(string objectId)
        {
            return await SendGraphGetRequest("/users/" + objectId + 
            "/$links/memberOf", null);
        }

Other reference:

  1. Can a B2C Access Token include app roles assigned to the user(github)
kavyaS
  • 8,026
  • 1
  • 7
  • 19
  • 1. If I'm not misunderstanding, this is what I've already done? As shown in the screenshots, I "assign the user to the same app roles from azure ad tenant by assigning users/groups to the created app roles", but no roles in the token from the B2C endpoint. If misunderstanding, could you please elaborate :) – Martin4ndersen Jun 21 '22 at 06:49
  • 2. "(...) in which the consumer can select required role during the signup process". The user (consumer) shouldn't be able to select their own role(s). This should be handled by admins in the (external) Azure ADs, i.e. to prevent users from making themselves admins. Similar discussion in the [issue](https://github.com/MicrosoftDocs/azure-docs/issues/43316#issuecomment-655546446) you linked to, but no solution. – Martin4ndersen Jun 21 '22 at 06:49
  • 3 & 4. Are the selected roles propagated from the Azure AD tenants to the Azure AD B2C tenant? It doesn't appear so, as I haven't found the roles in the UI for the user object and there is nothing in Audit logs about modified properties related to roles. If not propagated, I would then have to query the Azure AD tenants? – Martin4ndersen Jun 22 '22 at 06:25
0

Solved by passing through the roles claim:

  1. Open the TrustFrameworkExtensions.xml file and add the following ClaimType element with an identifier of roles to the ClaimsSchema element:
<ClaimType Id="roles">
  <DisplayName>Roles</DisplayName>
  <DataType>stringCollection</DataType>
  <UserInputType>Readonly</UserInputType>   
</ClaimType>       
  1. Add the OutputClaim element to the TechnicalProfile element used for configuring Azure AD as an identity provider:
<ClaimsProvider>
  <DisplayName>Common AAD</DisplayName>
  <TechnicalProfiles>
    <TechnicalProfile Id="AADCommon-OpenIdConnect">
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="roles" PartnerClaimType="roles" />
      </OutputClaims>
      ...
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>
  1. Save the TrustFrameworkExtensions.xml file.

  2. Open the relying party policy file, such as SignUpOrSignIn.xml, and add the OutputClaim element to the TechnicalProfile:

<RelyingParty>
  <DefaultUserJourney ReferenceId="SignUpOrSignIn" />
  <TechnicalProfile Id="PolicyProfile">
    <OutputClaims>
      <OutputClaim ClaimTypeReferenceId="roles" />
    </OutputClaims>
    ...
  </TechnicalProfile>
</RelyingParty>
  1. Save the policy file.

The token now includes roles:

{
  ...
  "roles": [
    "invoice-approver",
    "invoice-creator"
  ],
  ...
}
Martin4ndersen
  • 2,806
  • 1
  • 23
  • 32
  • Did you use a custom policy template to start with? If so, could you please link to it, and any documentation about implementing it that you found useful? I’m scared of custom policies (mainly the XML format confuses me) but I don’t think I can avoid them any longer as they are the gateway to so much required functionality. – user1063287 Jan 25 '23 at 18:44
  • I followed [Tutorial: Create user flows and custom policies in Azure Active Directory B2C](https://learn.microsoft.com/en-us/azure/active-directory-b2c/tutorial-create-user-flows?pivots=b2c-custom-policy) and started with SocialAccounts from the [Starter Pack](https://github.com/Azure-Samples/active-directory-b2c-custom-policy-starterpack). Configured CI/CD as described in [Deploy custom policies with Azure Pipelines](https://learn.microsoft.com/en-us/azure/active-directory-b2c/deploy-custom-policies-devops). Good luck! – Martin4ndersen Jan 27 '23 at 13:53
  • Is "roles" here custom user attribute? If in that case is it not necessary to reference it as "extension_roles" ? – Unnie Mar 20 '23 at 11:28
  • Not custom as the token from Azure AD includes a roles claim, ref: [Claims in access token](https://learn.microsoft.com/en-us/azure/active-directory/develop/access-tokens#claims-in-access-tokens). – Martin4ndersen Apr 03 '23 at 18:28