-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] TokenAcquisitionalways fails with MicrosoftIdentityWebChallengeUserException when called from a DelegatingHandler, but only on an Azure Web App. #516
Comments
Thanks @henriksen Do you have details about the MicrosoftIdentityWebChallengeUserException exception and the MsalUiRequiredException inner exception? We'll look at your repo. Thanks for sharing |
Yes, of course!
|
Oh, ok, @henriksen. So Microsoft.Identity.Web, in this particular context cannot find the user. (because the HttpContext is not available in Blazor, and probably the NavigationManager either) |
@henriksen: a possible idea (to try) might be to inject |
But why does it work fine locally? Or fine when I call it in the |
I'll try that, thanks! |
I don't know. maybe the execution context is different. |
Looks like the
|
Thanks for trying, @henriksen |
Interested in this bug as well. I can confirm that in a server side blazor app the user is not available in HttpContext reliably. Instead you might use AuthenticationStateProvider. You can add it via DI as follows:
later
Anyway, no luck so far. accessToken crashes in my code although authUser is available. Happy for any clues. |
@jmprieur let me ask this question to some folks, I'm not an expert in HttpClientFactory, the dev who worked on it is no longer on the team, and is now part of dotnet/runtime, so I'll need to familiarize myself a bit with it before I can understand if there's something going on. |
Just as an info: a valid HTTPContext seems to be available in _Host.cshtml of a Blazor Server App (see also first link below) For @henriksen and others who are interested in how to connect an Azure ADB2C with a Blazor server and a .Net Core Web api using the same ADB2C for auth, perhaps the two links below are helping you until this bug is fixed:
Instead of getting an access_token, id_token seems to work fine for authenticating against the web api. I am using "Microsoft.AspNetCore.Authentication.AzureADB2C.UI" on both sides (Blazor Server and API) as it is initialized by default in current Visual Studio when you create a Blazor Server/Web Api project. |
@andagon I don't think those would work in my case. Firstly, I'm not using B2C, but secondly, from my understanding the problem here is when getting a new access token for a downstream API. Having the original id-token or access-token from login wouldn't work, since the audience for those tokens are the Blazor app. I need to go to AAD, present one of those tokens and say "can I have an access token for this downstream service". This is the part that fails for me in the DelegatingHandler. I've got a workaround now, using custom typed clients that do the token acquisition in the actual client, but that means I can't use the already generated clients that relied on the DelegatingHandler. |
same issue here... @henriksen can you give more details on your workaround? much appriciated. |
@EdAlexander The workaround is basically using |
Very nice work Glenn! Hopefully the default tooling catches up in Blazor and things will get back to simple.
EAC Partners/317.762.3331
COVID-19: As always, EAC Partners is available to help your staff whether they are working at home or in the office. Remote assistance for your employees can be performed over the phone, through Microsoft Teams and/or Quick Assist on Windows 10.Together we can keep your workforce efficient through this health emergency.
From: Glenn F. Henriksen<mailto:notifications@github.com>
Sent: Friday, September 11, 2020 12:42 PM
To: AzureAD/microsoft-identity-web<mailto:microsoft-identity-web@noreply.github.com>
Cc: Edward Alexander<mailto:ed@eacpartners.com>; Mention<mailto:mention@noreply.github.com>
Subject: Re: [AzureAD/microsoft-identity-web] [Bug] TokenAcquisitionalways fails with MicrosoftIdentityWebChallengeUserException when called from a DelegatingHandler, but only on an Azure Web App. (#516)
@EdAlexander<https://github.com/EdAlexander> The workaround is basically using ITokenAccessor anywhere but in a DelegateHandler 😄 What I did was make a new Typed Client that just reuse the DTOs and Query objects from the generated clients and then does the token aquisition and HTTP calls itself.
I have an example here: https://gist.github.com/henriksen/fe8846ffb4a4373a95403597b285ed18
The BaseService does the generic heavy lifting and the UserService specifies the path to call and passes parameters in and results out. Hope you find it useful.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#516 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ADI6FERKMOLOKK37TPISNITSFJHPNANCNFSM4QOBBHDQ>.
|
@henriksen Is this still repro'ing with the latest version of Id.Web? wondering as we did a fix for anonymous controllers. |
Think I just Hit this. App was fine locally using a DelegatingHandler with GetAccessTokenForUserAsync(scope). But once I deployed to Azure it Failed with the much feared "No account or login hint was passed to the AcquireTokenSilent call." |
I think you can debug it locally by using an In-private browser session. |
worked fine locally in private. Failed inPrivate once I was up on azure. Moved it out of the delgating Handlerr and it was fine |
One additional Note if we use Redis as a distributed Cache its fine. But relying completely on InMemoryCache from withen Wouldnt be to suprised to find out Azure has something to prevent Stateful Delegating Handlers. |
closing, recommendation is to use |
Which version of Microsoft Identity Web are you using?
Microsoft Identity Web 0.3.0-preview
Where is the issue?
Is this a new or an existing app?
New Blazor part of existing ASP.NET Core MVC app, but reproducible in a small, plain Blazor app.
Repro
This is a wierd one. We have existing typed HttpClients and are using a
DelegatingHandler
to call.GetAccessTokenForUserAsync
and add the auth token from AAD on all requests. This works fine when injecting the client into, and using it from a MVC controller, but it fails with aMicrosoftIdentityWebChallengeUserException
every time when used from Blazor Server side and running on an Azure Web App, causing the page to go in an endless loop with the application page redirecting to AAD and the AAD authorization page redirecting back. Running locally, it works fine in both cases.If the token acquisition is included in the actual typed client, and not in the delegating handler, it works fine in all cases. It's just when called from a
DelegatingHandler
the call fails.TestClient.cs
- two versions of the typed client,AuthHandler.cs
Startup.cs
index.razor
For comparison, this works fine in all cases.
HomeController.cs
Expected behavior
When using the HTTP client, it should throw
MicrosoftIdentityWebChallengeUserException
once, redirect to AAD, authorize, reload the page and the HTTP client should get a token.Actual behavior
The
.GetAccessTokenForUserAsync
call throws aMicrosoftIdentityWebChallengeUserException
every time it is called in a delegating handler from a Blazor page, when running on Azure.Possible solution
No idea. Told ya it was an interesting one...
Additional context / logs / screenshots
I have a minimal repo here: https://github.com/henriksen/BlazorAuthRepo that includes the code mentioned above and consistently reproduces the error in our environment. It is based on the standard Blazor template and modified for
Microsoft.Identity.Web
0.3.0
. Add the correcttenantId
andclientId
inappSettings.json
, it also expects aAzureAd:ClientSecret
as a user secret (or in theappSettings.json
file).The sample repo uses Graph API for simplicity, but we're seeing the same problem calling our own APIs using our own defined scopes.
The app is set up to run on server, if changed to
ServerPrerendered
the Blazor page will flash the correct data once (from the pre-render), try to refresh, get the exception, redirect and then enter the infinite loop.The text was updated successfully, but these errors were encountered: