Skip to content
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

[OIDC] - Failed to authenticate - Regression after upgrading to .NET 7 #45187

Closed
1 task done
kbeaugrand opened this issue Nov 19, 2022 · 23 comments
Closed
1 task done
Assignees
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly feature-blazor-wasm-auth

Comments

@kbeaugrand
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

My application is a Blazor WebAssembly that is hosted by an ASP.Net server.

I configured my app to get access token for my backend with:

builder.Services.AddOidcAuthentication(options =>
{
    options.ProviderOptions.Authority = settings.Authority;
    options.ProviderOptions.MetadataUrl = settings.MetadataUrl.ToString();
    options.ProviderOptions.ClientId = settings.ClientId;
    options.ProviderOptions.ResponseType = "code";

    options.ProviderOptions.DefaultScopes.Add(settings.Scope);
});

builder.Services.AddApiAuthorization();

builder.Services.AddHttpClient("api", (sp, client) =>
{
    client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);
    client.DefaultRequestHeaders.Add("X-Version", "1.0");
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("api"));

It is working fine with this authentication and authorization flow running in .NET 6.

But after upgrading to .NET 7, I observe that the app fails to authenticate the user.
I receive the auth_code from the IDP and the client request after the token as expected:

image

N.B: if I disable the additional scope from the configuration. I can see that I have the ID_token, handled by the application, but failed to get token for requesting my backend (obviously)

Expected Behavior

My user should be authenticated. aht the access token handled by the app (as for .NET 6).

Steps To Reproduce

Configure the OIDC With adding an additional scope to the request and specify the authorization code flow:

    options.ProviderOptions.ResponseType = "code";
    options.ProviderOptions.DefaultScopes.Add(settings.Scope);

Exceptions (if any)

No response

.NET Version

7.0.100

Anything else?

No response

@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly feature-blazor-wasm-auth labels Nov 21, 2022
@javiercn
Copy link
Member

javiercn commented Nov 21, 2022

@kbeaugrand thanks for contacting us.

Can you give us more details? Is this happening in development or after publish?

Are there any errors on the console?

Can you tell us exactly what the error is? Is there anything on the screen?

Could you set the login level to debug in Microsoft.AspNetCore.Components.WebAssembly.Authentication? It will print additional information on the console to help troubleshoot the issue.

@javiercn javiercn added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Nov 21, 2022
@ghost
Copy link

ghost commented Nov 21, 2022

Hi @kbeaugrand. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@kbeaugrand
Copy link
Author

@kbeaugrand thanks for contacting us.

Can you give us more details? Is this happening in development or after publish?

Are there any errors on the console?

Can you tell us exactly what the error is? Is there anything on the screen?

Could you set the login level to debug in Microsoft.AspNetCore.Components.WebAssembly.Authentication? It will print additional information on the console to help troubleshoot the issue.

This is happening during the development

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Nov 21, 2022
@javiercn javiercn added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Nov 21, 2022
@ghost
Copy link

ghost commented Nov 21, 2022

Hi @kbeaugrand. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@javiercn
Copy link
Member

Can you tell us exactly what the error is? Is there anything on the screen?

Could you set the login level to debug in Microsoft.AspNetCore.Components.WebAssembly.Authentication? It will print additional information on the console to help troubleshoot the issue.

We still need info on these two questions

@kbeaugrand
Copy link
Author

Can you tell us exactly what the error is? Is there anything on the screen?

No error on the screen. No error on the browser console.

Could you set the login level to debug in Microsoft.AspNetCore.Components.WebAssembly.Authentication? It will print additional information on the console to help troubleshoot the issue.

I put these settings to the web assembly `appsettings.json file:

{
  "Logging": {
    "LogLevel": {
      "Microsoft.AspNetCore.Components.WebAssembly.Authentication": "Debug"
    }
  }
}

On the browser I still not have enough information to diagnose. Please see here the console output I have:
image

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Nov 21, 2022
@javiercn
Copy link
Member

@kbeaugrand thanks for the additional details.

Could you do it like this?
https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/testassets/Wasm.Authentication.Client/Program.cs#L15

This should do it, and you should be able to filter then

@kbeaugrand
Copy link
Author

Unfortunatly, not more:

image

@javiercn
Copy link
Member

@kbeaugrand thanks for the additional details.

I think something might be missing between the .NET 6.0 and .NET 7.0 update. Did you update the webassembly.authentication package?

Did you clean the bin and obj folders after the update?

@javiercn javiercn added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Nov 21, 2022
@ghost
Copy link

ghost commented Nov 21, 2022

Hi @kbeaugrand. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@javiercn javiercn removed the Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. label Nov 21, 2022
@kbeaugrand
Copy link
Author

@kbeaugrand thanks for the additional details.

I think something might be missing between the .NET 6.0 and .NET 7.0 update. Did you update the webassembly.authentication package?

Did you clean the bin and obj folders after the update?

All packages are up to date:

    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />

I do not need to clean bin and obj since the whole project will be running on docker and build from a fresh environnement...

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Nov 21, 2022
@javiercn
Copy link
Member

javiercn commented Nov 21, 2022

@kbeaugrand
Copy link
Author

@javiercn, How am I supposed to add a break point to the file you mentionned ? I'm using the build package. So I have only the .js file and it is minified... ,)

@javiercn
Copy link
Member

@kbeaugrand It's a bit tricky, but you can find the file in the sources (AuthenticationService.js).

Click the unminify button in Edge
image
And then search for completeSignIn or for completeSignIn-result inside the file.

@javiercn javiercn added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Nov 21, 2022
@ghost
Copy link

ghost commented Nov 21, 2022

Hi @kbeaugrand. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@kbeaugrand
Copy link
Author

@javiercn , I have the error :

image

The exception is:

Metadata does not contain property userinfo_endpoint

I already know this issue (because I was facing this issue with java spring security that needs also the userinfo_endpoint to be provided. This is because I'm using Azure AD B2C as the OIDC provider and ADB2C doesn't have the userinfo endpoint in its implementation...
This is quite strange because regarding the OIDC, this endpoint should not be mandatory since, we could use the id_token ...

Do you think I can bypass this issue ?

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Nov 21, 2022
@kbeaugrand
Copy link
Author

@javiercn, I see that the Authentication library is using the v1.11.5 version of oidc-client.

In the meantime, this package is no longer maintained and has been replaced by oidc-client-ts which was a new major version to support now TS (what a pity not to have taken advantage of the v7.0 release to update this package ;) ).

But what is important for me here is that during this migration, the loadUserInfo flag has been changed from true to false: https://authts.github.io/oidc-client-ts/interfaces/OidcClientSettings.html#loadUserInfo.
I would never have encountered this error with the new package ;)

See here some important notes for the migration:
https://github.com/authts/oidc-client-ts/blob/46fe10b1fe78f10d7764e900f19e98eed704d767/docs/migration.md#oidcclientsettings

@javiercn
Copy link
Member

I already know this issue (because I was facing this issue with java spring security that needs also the userinfo_endpoint to be provided. This is because I'm using Azure AD B2C as the OIDC provider and ADB2C doesn't have the userinfo endpoint in its implementation... This is quite strange because regarding the OIDC, this endpoint should not be mandatory since, we could use the id_token ...

Do you think I can bypass this issue ?

Thanks for the additional details. Let me make sure I understood. Are you using Microsoft.AspNetCore.Components.WebAssembly.Authentication to talk to an Azure AD B2C instance?

@kbeaugrand
Copy link
Author

I already know this issue (because I was facing this issue with java spring security that needs also the userinfo_endpoint to be provided. This is because I'm using Azure AD B2C as the OIDC provider and ADB2C doesn't have the userinfo endpoint in its implementation... This is quite strange because regarding the OIDC, this endpoint should not be mandatory since, we could use the id_token ...
Do you think I can bypass this issue ?

Thanks for the additional details. Let me make sure I understood. Are you using Microsoft.AspNetCore.Components.WebAssembly.Authentication to talk to an Azure AD B2C instance?

Yes right, and I know that I should rather be using the Msal library.
But in my case, I'm developing a Web App that should take an OIDC provider. The AD B2C is only used for demonstration purpose...

@javiercn
Copy link
Member

javiercn commented Nov 22, 2022

Yes right, and I know that I should rather be using the Msal library.
But in my case, I'm developing a Web App that should take an OIDC provider. The AD B2C is only used for demonstration purpose...

Yes, we do have a dedicated solution for AAD and AAD B2C, which is what we recommend in these cases. Our auth support can't effectively cover the particularities of every auth provider in the land without exposing all the details of the underlying library. We do want to keep the underlying JavaScript library as an implementation detail, as we might change it or even remove it in future versions.

For example, we want to enable you to BYOJ (Bring Your Own JavaScript) so that you can tweak the code to cater to your providers particularities. For example, like in the project below:

https://github.com/javiercn/BlazorWasmMsalSample/tree/main/BlazorWasmMsalSample/Client

In the meantime, this package is no longer maintained and has been replaced by oidc-client-ts which was a new major version to support now TS (what a pity not to have taken advantage of the v7.0 release to update this package ;) ).

We are aware of this fact; however, we have a plan in place in case we need to patch it and we are looking at options here in the future.

@kbeaugrand
Copy link
Author

Thank you for this sample @javiercn .

I think I will add my own javascript client (using the oidc-client-ts) as a workaround for this issue.
I saw that you added a new issue regarding the missing logs, good idea, this will help for the future ;)

Since the original issue wasn't due to this project, you might close this issue.

Many thanks @javiercn for your help...

@javiercn
Copy link
Member

@kbeaugrand No problem.

It was good for us to dig into this as we found a gap in our logging, that's something we made emphasis to improve on .NET 7.0 as it was one of the biggest issues when trying to troubleshoot problems like this, so this is hopefully a gap we can close.

@javiercn
Copy link
Member

Closing since we have a separate issue for the follow-up work.

@javiercn javiercn removed investigate Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Nov 22, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Dec 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly feature-blazor-wasm-auth
Projects
None yet
Development

No branches or pull requests

4 participants