Cannot access the management API #1971
Replies: 6 comments 4 replies
-
|
@frenetisch-applaudierend did your .NET application handles the OIDC interaction internally? (Since we do not have a .Net SDK yet, I assume the sign-in flow is handled internally by the IdentityModal you pasted here.)
Like our React SDK. This is the same as you set for the OidcClientOptions so far. However when trying to retrieve a specific access token for some API. You will have to let the Logto server know which API resource's access token you are asking for. You will only get the default access_token from the initial sign-in authorization flow. You will have to ask for an access_token exclusively for the API using the refresh token returned the first time. That might be the gap. It would be helpful if you could get the exact /auth/token request body. We can check if the resource param is present. Let me know if these make any sense to you. |
Beta Was this translation helpful? Give feedback.
-
|
This is where I can locate that the auth code is redeemed and exchanged for a token. As you can see no resource is being passed. So the default access token you received is not in JWT format, also not intent for any custom API use. Looks like the IdentityModel allows you to pass in additional params to the refreshToken request just like we did in our SDKs: Once you first login and get the refresh token from the token response. Can you try to call this RefreshTokenAsync with additional back-channel params: sth like: var refreshResult = await _oidcClient.RefreshTokenAsync(currentRefreshToken, { resource: "https://api.logto.io"} );The will ask Logto to issue a new access token exclusively for the usage of the management API. See how it goes. |
Beta Was this translation helpful? Give feedback.
-
|
Interesting this works well for me with the One more thing to check, make sure the resource is passed properly at the very beginning auth request. Can you check the signIn URI being generated Also, looks like IdentityModal Client has a refreshTokenAsync method? |
Beta Was this translation helpful? Give feedback.
-
|
@frenetisch-applaudierend how does it go? |
Beta Was this translation helpful? Give feedback.
-
|
Same issue with latest docker image: Unauthorized. Please check credentials and its scope |
Beta Was this translation helpful? Give feedback.
-
Using Logto with ASP.NET Core (MVC) and Access Tokens as JWTsI faced a similar issue when integrating Logto with an ASP.NET Core MVC application. I tested this solution in .NET 8 and .NET 9. ProblemInitially, I tried the official Logto SDK:
Inside the configuration, the However, in practice:
I tried disabling the property responsible for fetching user info: builder.Services.AddLogtoAuthentication(options => {
options.GetClaimsFromUserInfoEndpoint = false;
});Even though the default is already SolutionInstead of the Logto SDK, I switched to the generic OIDC middleware:
This means:
Here’s the key detail:
Step-by-Step Implementation1. Configure Authentication Schemesbuilder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.HttpOnly = true;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
})
.AddOpenIdConnect(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = "<logto-endpoint>"; // e.g. https://your-logto-domain/oidc
options.ClientId = "<app-id>";
options.ClientSecret = "<app-secret>";
options.ResponseType = "code";
options.SaveTokens = true;
// Request scopes
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("roles");
// Inject the `resource` parameter during token exchange
options.Events = new OpenIdConnectEvents
{
OnAuthorizationCodeReceived = async ctx =>
{
ctx.TokenEndpointRequest?.Parameters.Add("resource", "<api-resource>");
await Task.CompletedTask;
}
};
});2. Notes on Refresh TokensIf you need a refresh token:
ResultAfter making these changes, the access token returned by Logto is a JWT instead of an opaque string. This lets you validate tokens directly in your API using standard JWT middleware, without relying on the Debugging ExampleYou can test this setup and confirm that tokens and claims are being issued correctly by adding a debug endpoint: app.MapGet("/debug", async (HttpContext ctx) =>
{
var idToken = await ctx.GetTokenAsync("id_token");
var accessToken = await ctx.GetTokenAsync("access_token");
var refreshToken = await ctx.GetTokenAsync("refresh_token");
var claims = ctx.User.Claims.Select(c => new { c.Type, c.Value });
var roles = ctx.User.Claims
.Where(claim => claim.Type == ClaimTypes.Role)
.Select(claim => claim.Value)
.ToList();
return Results.Json(new
{
IdToken = idToken,
AccessToken = accessToken,
RefreshToken = refreshToken,
Claims = claims,
Roles = roles
});
}).RequireAuthorization();Visiting |
Beta Was this translation helpful? Give feedback.




Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to access the management api from a .NET sample application adapted from IdentityModel.OidcClient.Samples.
Logto is running locally in docker, on http://localhost:3001. I have setup an initial admin user (alice) and added a native application.
Using the following configuration of OidcClient I can get an identity token as well as an access token:
The decoded identity token JWT looks like this:
The access token does not seem to be a JWT.
I can pass the access token to
/oidc/meto get user information.When passing the access token to
/api/usershowever I get the following error (401):If I pass the identity token the error message is different (401):
The documentation mentions that the
audmust be "https://api.logto.io", but I don't know how to get such a token. Theaudseems always to match the application id, and using "https://api.logto.io" as application id does also not work, since there is no such app.Any help to what I'm doing wrong is much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions