question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Unable to get access token for multiple scopes

See original GitHub issue

Which Version of MSAL are you using ? MSAL 4.3.1

Platform .NET Core 2.2

What authentication flow has the issue?

  • Desktop / Mobile
    • Interactive
    • Integrated Windows Auth
    • Username Password
    • Device code flow (browserless)
  • Web App
    • Authorization code
    • OBO
  • Web API
    • OBO

Other? - please describe;

Is this a new or existing app? This is a new app or experiment that I’m doing by modifying the following sample project. https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2/tree/master/2. Web API now calls Microsoft Graph

I would like to get access token OBO for Graph API and Dynamics CRM API by providing scopes together and getting access token back with multiple audiences, so that it can be used for both of them.

Repro

  1. Add API Permission on TodoListService-v2 for Dynamics CRM user_impersonation
  2. Make sure that it also has Microsoft Graph User.Read
  3. In TodoListController in TodoListService project, add Dynamics CRM user_impersonation scope to line 90: string[] scopes = { “https://graph.microsoft.com//User.Read”, “https://admin.services.crm.dynamics.com/user_impersonation” };
public async Task<string> CallGraphApiOnBehalfOfUser()
        {
            string[] scopes = { "https://graph.microsoft.com/User.Read", "https://admin.services.crm.dynamics.com/user_impersonation" };

            // we use MSAL.NET to get a token to call the API On Behalf Of the current user
            try
            {
                string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(scopes);
                dynamic me = await CallGraphApiOnBehalfOfUser(accessToken);
                return me.userPrincipalName;
            }
            catch (MsalUiRequiredException ex)
            {
                _tokenAcquisition.ReplyForbiddenWithWwwAuthenticateHeader(scopes, ex);
                return string.Empty;
            }
        }

Expected behavior Access token should be returned with multiple audiences.

Actual behavior Access token returned is only for audience for which the scope is mentioned first in the scopes array.

Possible Solution

Additional context/ Logs / Screenshots I’ve tried many combinations but none of them work: “user.read” “user_impersonation” “user.read user_impersonation” “https://graph.microsoft.com//User.Read”, “https://admin.services.crm.dynamics.com//user_impersonation” Tried double forward slashes, single slashes, nothing worked.

I’ve also tried with Postman and it has the same behavior, in that it returns the access token with audience only for the scope that is mentioned first.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
hpsincommented, Sep 18, 2019

Thanks for the link!

In general, multiple audience scenarios are a security risk, which is why we don’t currently expose a way of building it. The main concern here, that Ping leaves out, is that you must trust that every API and micro service receiving a multi audience will never be compromised. If they’re compromised and get a token that can be used to call another audience, they can - and now your data is at risk, and possibly your business if the token has write permissions.

We are working to propose an update to the JWT standards to build tokens that can safely be issued with multiple audiences - but that adds complexity on the client side.

At this time, the secure (and only) option is to get multiple tokens for multiple audiences. This is handled by the libraries including refreshing and caching.

1reaction
jmprieurcommented, Sep 18, 2019

@mohsinonxrm : this is not a limitation of MSAL, but of Azure AD. But I’ll inquire if there are plans to change that. The best practice is to get the tokens with the right scopes just before calling the API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I want access tokens with multiple scopes including http
An access token only works for one API. So you cannot include both MS Graph scopes and your API scopes in one token....
Read more >
I want to get multiple access token for my multiple scopes. ...
I've got an app registered with Azure AD. Registration includes one scope for MS Graph and five for my protected resources.
Read more >
Issues with Scopes assignment in Azure AD
Hi, There are 2 issues seen while issuing token from Azure AD OAuth2.0 as below: Following apps are registered in AD: Product-A with...
Read more >
Error requesting Access token with scope
I'm just experimenting with the platform and I was trying to setup an api+client and have some scopes associated with it.
Read more >
Getting error "One or more scopes are not configured for ...
I am trying to get a token for multiple scopes by calling springframework RestTemplate.exchange(…) Headers anmd body are set properly, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found