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

getaccesstoken err when granttype is client_credentials #19994

Closed
mrh520 opened this issue Jun 7, 2024 · 13 comments · Fixed by #20045
Closed

getaccesstoken err when granttype is client_credentials #19994

mrh520 opened this issue Jun 7, 2024 · 13 comments · Fixed by #20045
Assignees
Milestone

Comments

@mrh520
Copy link

mrh520 commented Jun 7, 2024

version 7.4.4 upgrade 8.1.3
RequestPath = connect/token

{
"Depth": 0,
"ClassName": "Volo.Abp.Domain.Entities.EntityNotFoundException",
"Message": "There is no such an entity. Entity type: Volo.Abp.Identity.IdentityUser, id: de8acc86-0046-4ea5-a3d6-3a11c865bfc1",
"Source": "Volo.Abp.Identity.Domain",
"StackTraceString": " at Volo.Abp.Identity.IdentityUserManager.GetByIdAsync(Guid id)\n at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)\n at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync()\n at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)\n at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed)\n at Volo.Abp.Identity.IdentityDynamicClaimsPrincipalContributorCache.<>c__DisplayClass23_0.<<GetAsync>b__0>d.MoveNext()\n--- End of stack trace from previous location ---\n at Volo.Abp.Caching.DistributedCache2.GetOrAddAsync(TCacheKey key, Func1 factory, Func1 optionsFactory, Nullable1 hideErrors, Boolean considerUow, CancellationToken token)\n at Volo.Abp.Identity.IdentityDynamicClaimsPrincipalContributorCache.GetAsync(Guid userId, Nullable1 tenantId)\n at Volo.Abp.Identity.IdentityDynamicClaimsPrincipalContributor.ContributeAsync(AbpClaimsPrincipalContributorContext context)",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"HResult": -2146233088,
"HelpURL": null
}

@maliming maliming self-assigned this Jun 7, 2024
@maliming
Copy link
Member

maliming commented Jun 8, 2024

hi

Can you share the code to reproduce?

There should be no userId in client_credentials flow.

@mrh520
Copy link
Author

mrh520 commented Jun 11, 2024

Thanks for reply.

I just rewrote this method

public class AppTokenController : TokenController
{
protected override async Task HandleClientCredentialsAsync(OpenIddictRequest request)
{
// Note: the client credentials are automatically validated by OpenIddict:
// if client_id or client_secret are invalid, this action won't be invoked.
var application = await ApplicationManager.FindByClientIdAsync(request.ClientId);
if (application == null)
{
throw new InvalidOperationException(L["TheApplicationDetailsCannotBeFound"]);
}

    // Create a new ClaimsIdentity containing the claims that
    // will be used to create an id_token, a token or a code.
    var identity = new ClaimsIdentity(
        TokenValidationParameters.DefaultAuthenticationType,
        OpenIddictConstants.Claims.PreferredUsername,
        OpenIddictConstants.Claims.Role);

    // The Subject and PreferredUsername will be removed by <see cref="RemoveClaimsFromClientCredentialsGrantType"/>.
    // Use the client_id as the subject identifier.
    identity.AddClaim(OpenIddictConstants.Claims.Subject, await ApplicationManager.GetClientIdAsync(application));
    identity.AddClaim(OpenIddictConstants.Claims.PreferredUsername, await ApplicationManager.GetDisplayNameAsync(application));
    identity.AddClaim(OpenIddictApplicationConstants.Claims.CustomerId, await ApplicationManager.GetCustomerIdAsync(application) ?? "");

    // Note: In the original OAuth 2.0 specification, the client credentials grant
    // doesn't return an identity token, which is an OpenID Connect concept.
    //
    // As a non-standardized extension, OpenIddict allows returning an id_token
    // to convey information about the client application when the "openid" scope
    // is granted (i.e specified when calling principal.SetScopes()). When the "openid"
    // scope is not explicitly set, no identity token is returned to the client application.

    // Set the list of scopes granted to the client application in access_token.
    var principal = new ClaimsPrincipal(identity);

    principal.SetScopes(request.GetScopes());
    principal.SetResources(await GetResourcesAsync(request.GetScopes()));

    await OpenIddictClaimsPrincipalManager.HandleAsync(request, principal);

    return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
}

}

@maliming
Copy link
Member

Make sure there is no userId claim in your principal.

@mrh520
Copy link
Author

mrh520 commented Jun 11, 2024

I did not find userId in the rewrite method debugging

@maliming
Copy link
Member

Please share the full error stack or a simple test project.

@mrh520
Copy link
Author

mrh520 commented Jun 11, 2024

thinks.
this error cannot be replicated locally ,no error log was recorded for local debugging.
Can you determine whether to search user information according to granttype

@maliming maliming removed their assignment Jun 11, 2024
@maliming
Copy link
Member

There is no user concept in the client_credentials flow.

@mrh520
Copy link
Author

mrh520 commented Jun 12, 2024

claim type sub mapTo userId ?

@mrh520
Copy link
Author

mrh520 commented Jun 14, 2024

Make sure there is no userId claim in your principal.

image

@maliming
Copy link
Member

maliming commented Jun 14, 2024

This seems to be a problem. I will find a way to fix this.

@maliming maliming self-assigned this Jun 14, 2024
@mrh520
Copy link
Author

mrh520 commented Jun 14, 2024

will this issue be resolved in the future?

@maliming
Copy link
Member

See #20045

@maliming maliming added this to the 8.3-preview milestone Jun 14, 2024
@mrh520
Copy link
Author

mrh520 commented Jun 14, 2024

thank you

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

Successfully merging a pull request may close this issue.

2 participants