[BUG][Msal Provider] mgt-msal-provider connection suddenly blocked by browser
Describe the bug
As of May 30th the
To Reproduce Steps to reproduce the behavior:
- Create new .NET Core MVC application
- Create Azure App Registration
- Redirect URIs https://localhost:5001/signin-oidc AND https://localhost:5001/
- Select Access tokens AND ID tokens
- Fill out solution's appsettings.json file section AzureAD with appropriate info
- Place library import via mgt-loader tag in html head tag
- Place provider component and login component in index.cshtml
- Run application
- 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
Screenshots
Errors and issues visible in console
Issue in console expanded to show problem calls from 
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.
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 🙌
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.
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.
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:

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

Also, try setting "allowPublicClient" to "True" on your manifest in the app registration
I believe so, the Web redirect URIs have been deleted and replaced with SPAs
Same behavior with "allowPublicClient" set to True or False.
Screenshot:

@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;
});
@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.
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.
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.