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

Token Introspection for reference token in .Net 7 Core Web API #171

Open
ILoveCoding1100 opened this issue Feb 17, 2023 · 3 comments
Open

Comments

@ILoveCoding1100
Copy link

ILoveCoding1100 commented Feb 17, 2023

Environment
I started the app in Visual Studio on my local PC. I'm using .Net 7 with IdentityModel.AspNetCore.OAuth2Introspection" Version="6.1.0"

Question 1
If I use the following code:
builder.Services.AddAuthentication("token").AddOAuth2Introspection("token", options => { options.Authority = authority; options.ClientId = clientId; options.ClientSecret = clientSecret;
If I call end endpoint with postman (with an reference token). I get the following error:

System.InvalidOperationException: Discovery endpoint https://xxx/as/introspect.oauth2 is unavailable: Error connecting to https://xxx/as/introspect.oauth2/.well-known/openid-configuration: Not Found
at IdentityModel.AspNetCore.OAuth2Introspection.PostConfigureOAuth2IntrospectionOptions.GetIntrospectionEndpointFromDiscoveryDocument(OAuth2IntrospectionOptions options)
at IdentityModel.AspNetCore.OAuth2Introspection.PostConfigureOAuth2IntrospectionOptions.InitializeIntrospectionClient(OAuth2IntrospectionOptions options)
at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionHandler.LoadClaimsForToken(String token, HttpContext context, AuthenticationScheme scheme, OAuth2IntrospectionEvents events, OAuth2IntrospectionOptions options)
at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionHandler.<>c__DisplayClass8_0.<b__2>d.MoveNext()
--- End of stack trace from previous location ---
at IdentityModel.AspNetCore.OAuth2Introspection.OAuth2IntrospectionHandler.HandleAuthenticateAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.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 know it should work. Because other applications are using this token introspection endpoint as well. Am I missing something?

I added this snippets to my code:

builder.Services.AddAuthentication("token").AddOAuth2Introspection("token", options => { options.Authority = authority; options.ClientId = clientId; options.ClientSecret = clientSecret;

if (isAuthenticationEnabled) { app.UseAuthentication(); }

[Authorize] above my Controller
Is that all to get the authentication up and running in principle?

Question 2
Is there a difference between:
services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme) .AddOAuth2Introspection(options =>

and

builder.Services.AddAuthentication("token").AddOAuth2Introspection("token", options =>

Question 3
In the end I need a user context for a valid reference token.
The following article describes how to get user information. What is not clear to me is which approach to use now? The one from Question 1 or the approach from the following article.
https://identitymodel.readthedocs.io/en/latest/client/introspection.html

@brockallen
Copy link
Member

Do you have a call stack anywhere that you can share?

@ILoveCoding1100 ILoveCoding1100 changed the title I get a 500 and I'm not able to see an error Token Introspection for reference token in .Net 7 Core Web API Feb 23, 2023
@ILoveCoding1100
Copy link
Author

@brockallen I updated the post.

@brockallen
Copy link
Member

System.InvalidOperationException: Discovery endpoint https://xxx/as/introspect.oauth2 is unavailable: Error connecting to https://xxx/as/introspect.oauth2/.well-known/openid-configuration: Not Found

As you can see, this is the problem. Your app can't reach that URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants