sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

AadHttpClient or MSGraphClient does not work with multiple live accounts

Open edijsf opened this issue 8 years ago • 51 comments

Category

  • [ ] Question
  • [ ] Typo
  • [x] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

When multiple accounts found user interaction for choosing correct account should be provided

Observed Behavior

When multiple live account logged in or when current service account does not match any live account you get error.

Steps to Reproduce

When you are logged in SharePoint where SFPX web part exists that connects to Azure services wither using AadHttpClient or MSGraphClient client and check it works fine. Then open in new browser tab Azure portal do sign-out and log in with different account and switch to SharePoint tab and refresh. Or when multiple live accounts found you will get similar issue.

Following errors are not processed. Error in case of multiple live accounts found: AADSTS70002: Error validating credentials. AADSTS16000: Request is ambiguous: multiple user identities are available for the current request Error in case of account mismatch AADSTS50058: A silent sign-in request was sent but none of the currently signed in user(s) match the requested login hint

Proposed solution

Solution @microsoft/sp-http

// AdalAuthContext.js
67: AdalAuthContext.prototype._fetchAccessTokenSilent = function (resourceEndpoint) {`
.
.
73: if (_this._authContext._getItem(_this._authContext.CONSTANTS.STORAGE.ERROR) === 'interaction_required') {

Only one error is processed "interaction_required", but should process also when above mentioned errors are found.

edijsf avatar Sep 25 '18 06:09 edijsf

They can also avoid the login prompt by adding the "extraQueryParamter" to config of the "AuthenticationContext" constructor that hints at the current user.

var authContext = new AuthenticationContext({
    clientId: clientId,
    tenant: aadTenantId,
    redirectUri: window.location.origin + '/_forms/spfxsinglesignon.aspx',
    extraQueryParameter: '[email protected]'
});

araver avatar Oct 17 '18 21:10 araver

Just ran into the same problem. Logged in with 2 microsoft account.

henkterharmsel avatar Oct 18 '18 08:10 henkterharmsel

This is how i did it using ADAL

async getToken(headers: Headers) {
        var tp = await this.context.aadTokenProviderFactory.getTokenProvider();
        var config = tp["_defaultConfiguration"];
        var user = this.context.pageContext.user.loginName;
        var aadInstanceUrl = config.aadInstanceUrl[length - 1] === "/" ? config.aadInstanceUrl : config.aadInstanceUrl + "/";
        var ctx = new AuthenticationContext({
            tenant: config.aadTenantId,
            clientId: config.servicePrincipalId,
            instance: aadInstanceUrl,
            redirectUri: config.redirectUri,
            extraQueryParameter: "login_hint=" + encodeURIComponent(user),
            loadFrameTimeout: 60000
        });

        var cu = ctx.getCachedUser();
        console.log("USER", cu, user, ctx);
        if (cu && cu.userName.toLowerCase() !== user.toLowerCase()) {
            ctx.clearCache();
        }
        var token = await this.acquireToken(ctx, this.resource);
        if(headers.has("Authorization")){
            headers.set("Authorization", "Bearer " + token)
        }else{
            headers.append("Authorization", "Bearer " + token);
        }
    }

    private acquireToken(ctx: AuthenticationContext, resource: string){
        return new Promise<string>((resolve, reject) => {
            ctx.acquireToken(resource, (message, token) =>{
                if(!token){
                    reject(message)
                }else{
                    resolve(token);
                }
            });

        });
    }

araver avatar Oct 18 '18 17:10 araver

As this is not really an end-user scenario as they typically have one account, how often you'd really run into this issue unless you are a consultant with multiple accounts... and in this case incognito and profiles would help. Are we missing something on this? Sure - it could be a better implementation, but is this really a common scenario?

VesaJuvonen avatar Nov 15 '18 18:11 VesaJuvonen

My only concern is for a client that might have a shared workstation and the users don't clear the browser cache.

araver avatar Nov 15 '18 19:11 araver

Well, we did have some end users running in this problem allready. Most of them having some kind of admin and a normal account. After some time they did understand what caused it and that a normal user won't bump into this problem, but it's quite hard to explain.

henkterharmsel avatar Nov 16 '18 09:11 henkterharmsel

It works for all browsers except IE 11 for us. We are using SPFx 1.6. Below is the error message we are getting. @VesaJuvonen, Problem is we have multiple O365 tenants in our company and users who still use IE11 will face this issue. It works in IE11 inprivate. But we can't ask all the users to use InPrivate mode.

I have noticed that it is not sending login_hint with authorize request.

AADSTS70002: Error validating credentials. AADSTS16000: Request is ambiguous: multiple user identities are available for the current request.

kasgit avatar Nov 19 '18 22:11 kasgit

We experienced the same problem with a regular user: in addition to our company tenant, he had added credentials for a school where he's attending some courses. And he experienced an error while using our web part, which is using the aadHttpClient to connect to a back-end API.

lucmoco avatar Dec 17 '18 14:12 lucmoco

@VesaJuvonen It seems to be a problem for some of our end-users ... although it seems to be an edge case and will not affect a majority of our users it still is a blocking issue for some of them. As far as I know we do not have a work-around at the moment (I work in the same team as @lucmoco).

ghost avatar Dec 17 '18 15:12 ghost

Any update on this? I believe we are seeing a similar issue on our end. Is there any way to force login_hint within SPFx without resorting to ADAL?

ghost avatar Dec 24 '18 20:12 ghost

@VesaJuvonen end users are having the problem as well. Imagine they used their private account (either Hotmail or Gmail) to log on a consumer service/B2C partner tenant (like I don't know the school board of their district typically) and they are logged in with their corporate account on the corp tenant at the same time.

baywet avatar Jan 15 '19 14:01 baywet

to follow up the workarounds @araver is suggesting might work for aadhttpclient, but not for graph client as the factory does not expose the getprovider method (at least not in 1.6.0/1.7.1). Setup:

  • invite a ms account to a tenant (any tenant) as external user The exact repo case is:
  • start with an in private browser
  • log on to OneDrive.live.com with the MSA
  • go to your SharePoint site with your extension or webpart that calls AAD secured APIs or the Graph, log in with your corp account
  • you get the error Also linking that for reference https://github.com/AzureAD/azure-activedirectory-library-for-js/issues/580

baywet avatar Jan 15 '19 20:01 baywet

Thanks everyone for your input on this. We are aware of this and looking into shipping fix for this as part of the 1.8 release coming in a matter of weeks rather than months... task is currently prioritized as 'must' thing to fix for the release.

VesaJuvonen avatar Jan 30 '19 21:01 VesaJuvonen

@VesaJuvonen : Our clients are facing this issue on a daily basis and we need solution for it as soon as possible. Is there any specific time by when we can expect 1.8 release which will get shipped with this issue as resolved.

MaheshakaMudli avatar Feb 25 '19 03:02 MaheshakaMudli

@VesaJuvonen : this is also a big issue for our client and as mentioned in an earlier post, it is impossible to explain this issue to a customer. What is the eta on the above mention 1.8 release ? :)

brojgaard avatar Feb 28 '19 10:02 brojgaard

According to:

SharePoint Dev Weekly - Episode 28 - 26th of February 2019 https://youtu.be/oYKBFF7KiKM

ETA 1.8 Release: Soon

ghost avatar Feb 28 '19 10:02 ghost

Thanks @michel-weber :)

brojgaard avatar Feb 28 '19 11:02 brojgaard

According to SharePoint Developer Monthly Community Call - March 2019 (https://www.youtube.com/watch?v=YntZdZy0NvM), SPFx 1.8 will be shipped SOON (This Month).

Going forward, for new tenants, root site will be a communication site!

akkhilohri avatar Mar 14 '19 17:03 akkhilohri

SPFX v1.8... Soon - as in, NOW ! 8 hrs ago...

kachihro avatar Mar 15 '19 04:03 kachihro

And it's Finally here!!

Announcing the General Availability of SharePoint Framework 1.8

Published March 14, 2019

@2toLeadMike @pkmelee337 @araver @kannansgit @lucmoco @michel-weber @MaheshakaMudli @evobis @baywet

akkhilohri avatar Mar 15 '19 12:03 akkhilohri