C
C#9mo ago
Tarkacode

✅ Auth0 and ASP.NET API

Hello 👋 I have difficulties while trying to implement Auth0 in ASP.NET API. I tried many things. Currently, I am doing it all over again but with simpler applications to understand what is wrong with my configurations. So I created a new ASP.NET API to try to reach an endpoint with the attribute [Authorize]. My API Controller looks like this: https://paste.mod.gg/vrwcfkvfpexf/0 And my configuration looks like this : https://paste.mod.gg/yoyyytvmdcwk/0 So if I try to reach the endpoint with Postman here : https://localhost:7113/api/AuthTest/private With a Bearer Token that I copy from my "Test API endpoint" in Auth UI (you can see the image below) Then I receive this response in Postman : " System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://philauth.eu.auth0.com/.well-known/openid-configuration'. ---> System.TypeLoadException: Could not load type 'Microsoft.IdentityModel.Json.JsonConvert' from assembly 'Microsoft.IdentityModel.Tokens, Version=7.0.2.0, Culture=neutral, PublicKeyToken=(IChangedThisBecauseIdkIfThisIsSensitive)'. at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.Microsoft.IdentityModel.Protocols.IConfigurationRetriever<Microsoft.IdentityModel.Protocols. " I thought my API could not reach the endpoint, so I made the "OpenIdConfig" endpoint in the controller but I can reach it with no problem. So I really don't know? Does someone have an idea to see where the error comes from, or how to debug it? Please 😁
BlazeBin - vrwcfkvfpexf
A tool for sharing your source code with the world!
BlazeBin - yoyyytvmdcwk
A tool for sharing your source code with the world!
No description
22 Replies
Florian Voß
Florian Voß9mo ago
the issue is that Microsoft.IdentityModel.Json.JsonConvert doesn't exist, definetly not in Microsoft.IdentityModel.Tokens namespace. Do you mean Newtonsoft.Json.JsonConvert instead? @Philémon
Tarkacode
Tarkacode9mo ago
Hmm sorry, I don't think I understand the question? I copied and pasted the error displayed in my Postman "500 Internal Server Error" response I don't know where this error response comes from
Florian Voß
Florian Voß9mo ago
try dotnet add package Newtonsoft.Json
Tarkacode
Tarkacode9mo ago
I installed Newtonsoft.Json package but I still have the error
Florian Voß
Florian Voß9mo ago
rebuild exact same error?
Tarkacode
Tarkacode9mo ago
Yes the same error I rebuilded too
Florian Voß
Florian Voß9mo ago
oh and gotta add using Newtonsoft.Json; whereever you are using the JsonConvert class
Tarkacode
Tarkacode9mo ago
I don't use JsonConvert 😬 Auth0 is doing it for me I think ?
Florian Voß
Florian Voß9mo ago
possibly 🤷‍♂️
Tarkacode
Tarkacode9mo ago
The full error is the following : System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://philauth.eu.auth0.com/.well-known/openid-configuration'. ---> System.TypeLoadException: Could not load type 'Microsoft.IdentityModel.Json.JsonConvert' from assembly 'Microsoft.IdentityModel.Tokens, Version=7.0.2.0, Culture=neutral, PublicKeyToken=(mykey)'. at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.Microsoft.IdentityModel.Protocols.IConfigurationRetriever<Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfiguration>.GetConfigurationAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel) --- End of inner exception stack trace --- at Microsoft.IdentityModel.Protocols.ConfigurationManager1.GetConfigurationAsync(CancellationToken cancel) at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync() at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync() at Microsoft.AspNetCore.Authentication.AuthenticationHandler1.AuthenticateAsync() at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context) I just changed the public key token (in the error message) because I don't know if this is sensitive information 😁 Omygod it works 🤣 Thank you @Florian Voß you gave me the idea of the nuget package problem It appears that I had too many non-needed packages, I removed them and it now works 🥳
Florian Voß
Florian Voß9mo ago
glad it works 😊 what did you do to fix? I could imagine the issue was that because you had newtonsoft package installed, the Microsoft internal code was trying to use Newtonsoft instead of STJ (System.Text.Json, the builtin serializer in .Net) which caused this issue. Did you throw Newtonsoft package out now to fix the issue? Then it doesn'T try to use JsonConvert class from Newtonsoft anymore and instead tries to use JsonSerializer from STJ. That would be a plausible explanation I guess 🤷‍♂️
Tarkacode
Tarkacode9mo ago
I removed two Json related packages. The first is the one that you said I should install, and the second one System.IdentityModel.Tokens.Jwt
Florian Voß
Florian Voß9mo ago
yep so this was in fact the issue
Tarkacode
Tarkacode9mo ago
And since i already had the problem before installing newtonsoft, its probably System.IdentityModel.Tokens.Jwt that caused the issue
Florian Voß
Florian Voß9mo ago
you were using System.IdentityModel which is old shit, instead of Microsoft.IdentityModel. The old shit wants to use Newtonsoft, the new stuff wants STJ
Tarkacode
Tarkacode9mo ago
But what i don't understand is, is that is it Auth0 on their website who say "1. Choose a JWT library As your API will be parsing JWT formatted access tokens, you will need to setup these capabilities on your API. You can navigate to jwt.io and choose from there. Remember to pick a library that support your selected signing algorithm." So i click on the jwt.io link, then i click on "See all libraries" and then I see "Install-Package System.IdentityModel.Tokens.Jwt" for .NET
Florian Voß
Florian Voß9mo ago
seems jwt.io is outdated here microsoft docs would tell you to use Microsoft.IdentityModel instead of System.IdentityModel
Tarkacode
Tarkacode9mo ago
Ok, and I guess that this Microsoft.IdentityModel is included in the base class library? Because I didn't have to install it myself, and it is not listed in the added Packages I am so happy that we found the solution, it has been 2 weeks since I work on this problem 🥳 🥳 🥳 System is included in my API by default then ?
Florian Voß
Florian Voß9mo ago
I think so yes, it should be part of Microsoft.AspNetCore.App I believe yes, by default you have ImpliciteUsings enabled. You can see <ImpliciteUsings>enabled</ImpliciteUsings> in your .csproj. System is one of those implicitely used namespaces
Tarkacode
Tarkacode9mo ago
Very nice 😁 Thank you again!!
Florian Voß
Florian Voß9mo ago
glad I could help 😊 make sure to /close the thread
Tarkacode
Tarkacode9mo ago
Yes sure, thanks!
Want results from more Discord servers?
Add your server
More Posts
❔ Can't get the source generators tutorial to workAs the title says, I'm following the source generators tutorial that's here : https://learn.microsof❔ EF Core Adding "1" to Column NameHello, I got a problem while configuring a one-to-many relationship in .NET EF Core. This is the p✅ Im very(!) confused about the typeof method(not even sure if method is the correct name)When looping through an enum, we use Enum.GetValues to retrieve an array of each value in the Enum. ❔ FreeImage does not contain a definition for GetBitmapEvery tutorial I find says ```C++ Bitmap bitmap = FreeImage.GetBitmap(dib); ``` should be a worki❔ ngl ion really get what this doesstring[,] beach = { {"" , "" , "" , ""}, {"farmer", "wolf", "rabbit", "carrot"} }; so arent✅ shortcutwhat's the shortest way of making a variable? for example: ```css /// NOT VALID CODE (i think) Form ❔ [WINFORMS] Make textbox scale vertically according to its content.I want to make a component for displaying the update log of an app. The update log contains multiple❔ std::function-like varHey, I want to pass an function pointer (only void type) to a method which accepts generic arguments✅ "Object reference not set to an instance of"Title, I use a Interface to allow me to make a list of objects that require generic arguments, one pSaying error Interface is not accessible due to protection, but protection is public then why this eI'm developing MVC app using Controllers and Models But showing this error: Istudentrepository is Pu