microsoft-graph-toolkit icon indicating copy to clipboard operation
microsoft-graph-toolkit copied to clipboard

[BUG][Msal Provider] mgt-msal-provider connection suddenly blocked by browser

Open umaknow-william opened this issue 5 years ago • 10 comments

Describe the bug As of May 30th the fails to connect and receive tokens in a .NET Core MVC application. The connection appears to be blocked by the browser. The failures appeared suddenly and affect existing apps and newly created ones (eg: no changes in code or environment).

To Reproduce Steps to reproduce the behavior:

  1. Create new .NET Core MVC application
  2. Create Azure App Registration
    • Redirect URIs https://localhost:5001/signin-oidc AND https://localhost:5001/
    • Select Access tokens AND ID tokens
  3. Fill out solution's appsettings.json file section AzureAD with appropriate info
  4. Place library import via mgt-loader tag in html head tag
  5. Place provider component and login component in index.cshtml
  6. Run application
  7. See errors in sample project

Expected behavior Application should run and open browser without any errors or issues in the browsers console The provider should connect without issue and when clicking on the sign in button user should/can be signed in.

Screenshots Errors and issues visible in console consoleError Issue in console expanded to show problem calls from cookieIssue

Environment (please complete the following information):

  • Browser: tested on edge and chrome
  • Framework: .NET Core MVC (5.0)
  • Context: Web
  • Provider :MsalProvider

Additional context Have been using the mgt library and components in this type of application for weeks, first time seeing this issue, and haven't found any workaround yet.

umaknow-william avatar Jun 02 '21 14:06 umaknow-william

Hello umaknow-william, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

ghost avatar Jun 02 '21 14:06 ghost

Hi @umaknow-william! This looks like an issue with MSAL, which we will try and follow up on. In the mean time, I recommend you to switch over to MSAL2Provider. I was able to get your sample to work by making this change. You will have to change the redirect URIs in your app registration to be of type SPA (migration instructions are here), or create a new app registration.

amruthasrini avatar Jun 07 '21 23:06 amruthasrini

Hi @amrutha95, Thanks for looking into this. I thought I had already tried migrating to MSAL2 as a fix (didn't succeed), however it is very possible that I didn't set up the new App registration correctly. I will take another look right away, and reply for posterity.

umaknow-william avatar Jun 08 '21 15:06 umaknow-william

After following the migration procedure to MSAL2, there is a different error when attempting to sign in via the mgt-login component. It is unclear to me why the error mentions the client_secret as this is not given explicitly to the Provider by the app/developer. This is how I am declaring the components in the sample project:

<mgt-msal2-provider client-id="[app-registration-client-id]" authority="https://login.microsoftonline.com/[tenant-id]" scopes="user.read"> </mgt-msal2-provider> <mgt-login></mgt-login>

Error summary: VM13:1 POST https://login.microsoftonline.com/0a6bbd99-2bb9-419a-9531-2ed68cae955f/oauth2/v2.0/token 401 (Unauthorized) mgt.es6.js:1746 Uncaught (in promise) ServerError: invalid_client: 7000218 - [2021-06-09 14:07:36Z]: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.

Full error text: VM13:1 POST https://login.microsoftonline.com/0a6bbd99-2bb9-419a-9531-2ed68cae955f/oauth2/v2.0/token 401 (Unauthorized) (anonymous) @ VM13:1 (anonymous) @ mgt.es6.js:1768 step @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 __awaiter$2 @ mgt.es6.js:1728 FetchClient.sendPostRequestAsync @ mgt.es6.js:1768 (anonymous) @ mgt.es6.js:1755 step @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 __awaiter$1 @ mgt.es6.js:1743 NetworkManager.sendPostRequest @ mgt.es6.js:1755 (anonymous) @ mgt.es6.js:1755 step @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 __awaiter$1 @ mgt.es6.js:1743 BaseClient.executePostToTokenEndpoint @ mgt.es6.js:1755 (anonymous) @ mgt.es6.js:1756 step @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 fulfilled @ mgt.es6.js:1743 Promise.then (async) step @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 __awaiter$1 @ mgt.es6.js:1743 AuthorizationCodeClient.executeTokenRequest @ mgt.es6.js:1756 (anonymous) @ mgt.es6.js:1756 step @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 (anonymous) @ mgt.es6.js:1743 __awaiter$1 @ mgt.es6.js:1743 AuthorizationCodeClient.acquireToken @ mgt.es6.js:1756 (anonymous) @ mgt.es6.js:1771 step @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 __awaiter$2 @ mgt.es6.js:1728 RedirectHandler.handleCodeResponse @ mgt.es6.js:1771 (anonymous) @ mgt.es6.js:1774 step @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 fulfilled @ mgt.es6.js:1728 Promise.then (async) step @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 __awaiter$2 @ mgt.es6.js:1728 ClientApplication.handleHash @ mgt.es6.js:1774 (anonymous) @ mgt.es6.js:1774 step @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 __awaiter$2 @ mgt.es6.js:1728 ClientApplication.handleRedirectResponse @ mgt.es6.js:1774 (anonymous) @ mgt.es6.js:1774 step @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 (anonymous) @ mgt.es6.js:1728 __awaiter$2 @ mgt.es6.js:1728 ClientApplication.handleRedirectPromise @ mgt.es6.js:1774 (anonymous) @ mgt.es6.js:1775 (anonymous) @ mgt.es6.js:1774 __awaiter @ mgt.es6.js:1774 initProvider @ mgt.es6.js:1775 Msal2Provider @ mgt.es6.js:1775 initializeProvider @ mgt.es6.js:1775 firstUpdated @ mgt.es6.js:186 performUpdate @ mgt.es6.js:148 _enqueueUpdate @ mgt.es6.js:148 async function (async) _enqueueUpdate @ mgt.es6.js:148 requestUpdateInternal @ mgt.es6.js:148 initialize @ mgt.es6.js:148 initialize @ mgt.es6.js:186 UpdatingElement @ mgt.es6.js:148 LitElement @ mgt.es6.js:186 MgtBaseComponent @ mgt.es6.js:186 MgtBaseProvider @ mgt.es6.js:186 MgtMsal2Provider @ mgt.es6.js:1775 mgt.es6.js:1746 Uncaught (in promise) ServerError: invalid_client: 7000218 - [2021-06-09 14:07:36Z]: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'. Trace ID: f625798b-4c28-424b-97e7-da0d791ff000 Correlation ID: 1c6f95a1-e7a5-4b47-b4bd-b14eb720ac55 Timestamp: 2021-06-09 14:07:36Z - Correlation ID: 1c6f95a1-e7a5-4b47-b4bd-b14eb720ac55 - Trace ID: f625798b-4c28-424b-97e7-da0d791ff000 at ServerError.AuthError [as constructor] (mgt.es6.js:1746) at new ServerError (mgt.es6.js:1755) at ResponseHandler.validateTokenResponse (mgt.es6.js:1756) at AuthorizationCodeClient.<anonymous> (mgt.es6.js:1756) at step (mgt.es6.js:1743) at Object.next (mgt.es6.js:1743) at fulfilled (mgt.es6.js:1743)

Screenshot: newMGTError

umaknow-william avatar Jun 09 '21 14:06 umaknow-william

Hi @umaknow-william! Can you confirm that the redirect URIs are of SPA type? image

Also, try setting "allowPublicClient" to "True" on your manifest in the app registration

amruthasrini avatar Jun 09 '21 20:06 amruthasrini

I believe so, the Web redirect URIs have been deleted and replaced with SPAs Same behavior with "allowPublicClient" set to True or False. Screenshot: spaType

umaknow-william avatar Jun 10 '21 14:06 umaknow-william

@umaknow-william For me the following post helped: Using PKCE with ASP.NET CORE webapp and Azure AD

More precise the part with the AddMicrosoftIdentityWebApp code:

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(o => {
                    o.UsePkce = true;
                    o.ClientId = "<client_id>";
                    o.TenantId = "<tenant_id>";
                    o.Domain = "yourdomain.com";
                    o.Instance = "https://login.microsoftonline.com";
                    o.CallbackPath = "/signin-oidc";
                    o.ResponseType = "code";
                    var defaultBackChannel = new HttpClient();
                    defaultBackChannel.DefaultRequestHeaders.Add("Origin", "thisismyapp");
                    o.Backchannel = defaultBackChannel;
                });

Erw1n avatar Jun 16 '21 07:06 Erw1n

@nmetulev created a fork of your repo and got it to work by:

  • adding a blank HTML page called sso.html as a redirect uri for the MSAL2Provider flow.
  • splitting the redirect urls between Web and SPA like this to support both flows.
  • Additionally, adding a login hint to the msal2provider enables the single sign on scenario so the MSAL2Provider can just fetch the token silently for the specified login hint without the user having to log in again.

I took @Erw1n 's suggestion and migrated the azure AD flow to PKCE as well, with this configuration. I just added the code enabling Azure AD auth with PKCE here. Let me know if this works for you.

amruthasrini avatar Jun 18 '21 23:06 amruthasrini

Using @Erw1n 's suggestion by itself didn't work. I will implement and test the steps suggested in @amrutha95 's post and reply back. Thanks for all the assistance everyone.

umaknow-william avatar Jun 21 '21 14:06 umaknow-william

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Jul 03 '21 17:07 ghost