openiddict-core: Calling AuthenticateInteractivelyAsync result in error invalid_grant
Confirm you’ve already contributed to this project or that you sponsor it
- I confirm I’m a sponsor or a contributor
Version
5.0.1
Question
Below is the log of what is happening. The code is the same as in issue. I am not sure what is causing the error.
Authorization Controller
if ( request.IsAuthorizationCodeFlow () )
{
// Try to retrieve the user principal
var result = await HttpContext.AuthenticateAsync ( CookieAuthenticationDefaults.AuthenticationScheme );
if ( result == null || !result.Succeeded )
{
// If the client application requested promptless authentication,
// return an error indicating that the user is not logged in.
if ( request.HasPrompt ( Prompts.None ) )
{
return Forbid (
authenticationSchemes: OpenIddictServerAspNetCoreDefaults.AuthenticationScheme,
properties: new AuthenticationProperties ( new Dictionary<string, string>
{
[ OpenIddictServerAspNetCoreConstants.Properties.Error ] = Errors.LoginRequired,
[ OpenIddictServerAspNetCoreConstants.Properties.ErrorDescription ] = "The user is not logged in."
} ) );
}
// To avoid endless login -> authorization redirects, the prompt=login flag
// is removed from the authorization request payload before redirecting the user.
var prompt = string.Join ( " ", request.GetPrompts ().Remove ( Prompts.Login ) );
var parameters = Request.HasFormContentType ?
Request.Form.Where ( parameter => parameter.Key != Parameters.Prompt ).ToList () :
Request.Query.Where ( parameter => parameter.Key != Parameters.Prompt ).ToList ();
parameters.Add ( KeyValuePair.Create ( Parameters.Prompt, new StringValues ( prompt ) ) );
var redirectUri = Request.PathBase + Request.Path + QueryString.Create ( parameters );
return Challenge (
authenticationSchemes: CookieAuthenticationDefaults.AuthenticationScheme,
properties: new AuthenticationProperties
{
RedirectUri = redirectUri
} );
}
// Retrieve the profile of the logged in user.
var principal = result.Principal;
// Retrieve the application details from the database.
var application = await ApplicationManager.FindByClientIdAsync ( request.ClientId ) ??
throw new InvalidOperationException ( "Details concerning the calling client application cannot be found." );
// Retrieve the permanent authorizations associated with the user and the calling client application.
var subject = await UserManager.GetUserIdAsync ( principal );
var authorizations = await AuthorizationManager.FindAsync (
subject: subject,
client: await ApplicationManager.GetIdAsync ( application ),
status: Statuses.Valid,
type: AuthorizationTypes.Permanent,
scopes: request.GetScopes () ).ToListAsync ();
var consentType = await ApplicationManager.GetConsentTypeAsync ( application );
switch ( consentType )
{
// If the consent is external (e.g when authorizations are granted by a sysadmin),
// immediately return an error if no authorization can be found in the database.
case ConsentTypes.External when !authorizations.Any ():
return Forbid (
authenticationSchemes: OpenIddictServerAspNetCoreDefaults.AuthenticationScheme,
properties: new AuthenticationProperties ( new Dictionary<string, string>
{
[ OpenIddictServerAspNetCoreConstants.Properties.Error ] = Errors.ConsentRequired,
[ OpenIddictServerAspNetCoreConstants.Properties.ErrorDescription ] =
"The logged in user is not allowed to access this client application."
} ) );
// If the consent is implicit or if an authorization was found,
// return an authorization response without displaying the consent form.
case ConsentTypes.Implicit:
case ConsentTypes.External when authorizations.Any ():
case ConsentTypes.Explicit when authorizations.Any () && !request.HasPrompt ( Prompts.Consent ):
// Create the claims-based identity that will be used by OpenIddict to generate tokens.
var claimsIdentity = new ClaimsIdentity (
authenticationType: OpenIddictServerAspNetCoreDefaults.AuthenticationScheme );
/*
nameType: Claims.Name,
roleType: Claims.Role );
*/
var username = await UserManager.GetUsernameAsync ( principal );
var user = await UserManager.FindByUsernameAsync ( username );
// Add the claims that will be persisted in the tokens.
claimsIdentity.SetClaim ( Claims.Subject, user.Id )
//.SetClaim ( Claims.Email, userGetEmail )
.SetClaim ( Claims.Username, user.Username );
// Note: in this sample, the granted scopes match the requested scope
// but you may want to allow the user to uncheck specific scopes.
// For that, simply restrict the list of scopes before calling SetScopes.
claimsIdentity.SetScopes ( request.GetScopes () );
claimsIdentity.SetResources ( await ScopeManager.ListResourcesAsync ( claimsIdentity.GetScopes () ).ToListAsync () );
// Automatically create a permanent authorization to avoid requiring explicit consent
// for future authorization or token requests containing the same scopes.
var authorization = authorizations.LastOrDefault ();
authorization ??= await AuthorizationManager.CreateAsync (
identity: claimsIdentity,
subject: user.Id.ToString (),
client: await ApplicationManager.GetIdAsync ( application ),
type: AuthorizationTypes.Permanent,
scopes: claimsIdentity.GetScopes () );
claimsIdentity.SetAuthorizationId ( await AuthorizationManager.GetIdAsync ( authorization ) );
claimsIdentity.SetDestinations ( GetDestinations );
return SignIn ( new ClaimsPrincipal ( claimsIdentity ), OpenIddictServerAspNetCoreDefaults.AuthenticationScheme );
//break;
// At this point, no authorization was found in the database and an error must be returned
// if the client application specified prompt=none in the authorization request.
case ConsentTypes.Explicit when request.HasPrompt ( Prompts.None ):
case ConsentTypes.Systematic when request.HasPrompt ( Prompts.None ):
return Forbid (
authenticationSchemes: OpenIddictServerAspNetCoreDefaults.AuthenticationScheme,
properties: new AuthenticationProperties ( new Dictionary<string, string>
{
[ OpenIddictServerAspNetCoreConstants.Properties.Error ] = Errors.ConsentRequired,
[ OpenIddictServerAspNetCoreConstants.Properties.ErrorDescription ] =
"Interactive user consent is required."
} ) );
}
}
Output
01/14/24 11:56:14 771 [INFO] The request URI matched a server endpoint: Authorization.
01/14/24 11:56:14 773 [INFO] The authorization request was successfully extracted: {
"client_id": "core_api_client",
"redirect_uri": "http://localhost:7000/",
"response_type": "code",
"scope": "openid gtapi",
"nonce": "eBj-G8hrCf5lmlrzTGKvHxE2yJY9DcRPSnK692NMtXk",
"code_challenge": "wHhnsm0VVO7t4gttG84I4eoRc6Q8cfIRBtyzwc5EP2E",
"code_challenge_method": "S256",
"state": "566rjklB0G3f93wV-XXGcK5ubqHGSE5bdzcbgHdtW1I",
"prompt": ""
}.
01/14/24 11:56:14 827 [INFO] Executed DbCommand (0ms) [Parameters=[@__identifier_0='?' (Size = 15)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationType", "o"."ClientId", "o"."ClientSecret", "o"."ClientType", "o"."ConcurrencyToken", "o"."ConsentType", "o"."DisplayName", "o"."DisplayNames", "o"."JsonWebKeySet", "o"."Permissions", "o"."PostLogoutRedirectUris", "o"."Properties", "o"."RedirectUris", "o"."Requirements", "o"."Settings"
FROM "OpenIddictApplications" AS "o"
WHERE "o"."ClientId" = @__identifier_0
LIMIT 1
01/14/24 11:56:14 837 [INFO] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ConcurrencyToken", "o"."Description", "o"."Descriptions", "o"."DisplayName", "o"."DisplayNames", "o"."Name", "o"."Properties", "o"."Resources"
FROM "OpenIddictScopes" AS "o"
WHERE "o"."Name" = 'gtapi'
01/14/24 11:56:14 842 [INFO] The authorization request was successfully validated.
01/14/24 11:56:14 845 [INFO] Executing endpoint 'Gt.IDP.Controllers.AuthorizationController.Authorize (Gt.IDP)'
01/14/24 11:56:14 847 [INFO] Route matched with {action = "Authorize", controller = "Authorization", page = ""}. Executing controller action with signature System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.IActionResult] Authorize(System.String) on controller Gt.IDP.Controllers.AuthorizationController (Gt.IDP).
01/14/24 11:56:53 819 [INFO] Executed DbCommand (0ms) [Parameters=[@__subject_0='?' (Size = 1), @__status_1='?' (Size = 5), @__type_2='?' (Size = 9), @__key_3='?' (Size = 36)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationId", "o"."ConcurrencyToken", "o"."CreationDate", "o"."Properties", "o"."Scopes", "o"."Status", "o"."Subject", "o"."Type", "o0"."Id", "o0"."ApplicationType", "o0"."ClientId", "o0"."ClientSecret", "o0"."ClientType", "o0"."ConcurrencyToken", "o0"."ConsentType", "o0"."DisplayName", "o0"."DisplayNames", "o0"."JsonWebKeySet", "o0"."Permissions", "o0"."PostLogoutRedirectUris", "o0"."Properties", "o0"."RedirectUris", "o0"."Requirements", "o0"."Settings"
FROM "OpenIddictAuthorizations" AS "o"
LEFT JOIN "OpenIddictApplications" AS "o0" ON "o"."ApplicationId" = "o0"."Id"
INNER JOIN "OpenIddictApplications" AS "o1" ON "o0"."Id" = "o1"."Id"
WHERE "o"."Subject" = @__subject_0 AND "o"."Status" = @__status_1 AND "o"."Type" = @__type_2 AND "o1"."Id" = @__key_3
01/14/24 11:57:11 307 [INFO] CustodianGateway.GetByUserNameAsync enter
01/14/24 11:57:11 512 [INFO] DataSettingsGateway()
01/14/24 11:57:11 514 [INFO] DataSettingsGateway.GetPrimaryConnection enter
01/14/24 11:57:11 514 [INFO] DataSettingsGateway.GetPrimaryConnection exit
01/14/24 11:57:15 338 [INFO] CustodianGateway.GetByUserNameAsync exit
01/14/24 11:57:24 373 [INFO] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ConcurrencyToken", "o"."Description", "o"."Descriptions", "o"."DisplayName", "o"."DisplayNames", "o"."Name", "o"."Properties", "o"."Resources"
FROM "OpenIddictScopes" AS "o"
WHERE "o"."Name" IN ('openid', 'gtapi')
01/14/24 11:57:39 520 [INFO] Executing SignInResult with authentication scheme (OpenIddict.Server.AspNetCore) and the following principal: System.Security.Claims.ClaimsPrincipal.
01/14/24 11:57:39 588 [INFO] Executed DbCommand (0ms) [Parameters=[@__key_0='?' (Size = 36)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationType", "o"."ClientId", "o"."ClientSecret", "o"."ClientType", "o"."ConcurrencyToken", "o"."ConsentType", "o"."DisplayName", "o"."DisplayNames", "o"."JsonWebKeySet", "o"."Permissions", "o"."PostLogoutRedirectUris", "o"."Properties", "o"."RedirectUris", "o"."Requirements", "o"."Settings"
FROM "OpenIddictApplications" AS "o"
WHERE "o"."Id" = @__key_0
LIMIT 1
01/14/24 11:57:39 600 [INFO] Executed DbCommand (2ms) [Parameters=[@__key_0='?' (Size = 36)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationId", "o"."ConcurrencyToken", "o"."CreationDate", "o"."Properties", "o"."Scopes", "o"."Status", "o"."Subject", "o"."Type"
FROM "OpenIddictAuthorizations" AS "o"
WHERE "o"."Id" = @__key_0
LIMIT 1
01/14/24 11:57:39 878 [INFO] Executed DbCommand (8ms) [Parameters=[@p0='?' (Size = 36), @p1='?' (Size = 36), @p2='?' (Size = 36), @p3='?' (Size = 36), @p4='?' (DbType = DateTime), @p5='?' (DbType = DateTime), @p6='?', @p7='?', @p8='?' (DbType = DateTime), @p9='?', @p10='?' (Size = 5), @p11='?' (Size = 1), @p12='?' (Size = 18)], CommandType='Text', CommandTimeout='30']
INSERT INTO "OpenIddictTokens" ("Id", "ApplicationId", "AuthorizationId", "ConcurrencyToken", "CreationDate", "ExpirationDate", "Payload", "Properties", "RedemptionDate", "ReferenceId", "Status", "Subject", "Type")
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12);
01/14/24 11:57:39 972 [INFO] Executed DbCommand (0ms) [Parameters=[@__key_0='?' (Size = 36)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationType", "o"."ClientId", "o"."ClientSecret", "o"."ClientType", "o"."ConcurrencyToken", "o"."ConsentType", "o"."DisplayName", "o"."DisplayNames", "o"."JsonWebKeySet", "o"."Permissions", "o"."PostLogoutRedirectUris", "o"."Properties", "o"."RedirectUris", "o"."Requirements", "o"."Settings"
FROM "OpenIddictApplications" AS "o"
WHERE "o"."Id" = @__key_0
LIMIT 1
01/14/24 11:57:39 975 [INFO] Executed DbCommand (0ms) [Parameters=[@__key_0='?' (Size = 36)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationId", "o"."ConcurrencyToken", "o"."CreationDate", "o"."Properties", "o"."Scopes", "o"."Status", "o"."Subject", "o"."Type"
FROM "OpenIddictAuthorizations" AS "o"
WHERE "o"."Id" = @__key_0
LIMIT 1
01/14/24 11:57:39 995 [INFO] Executed DbCommand (0ms) [Parameters=[@__identifier_0='?' (Size = 44)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationId", "o"."AuthorizationId", "o"."ConcurrencyToken", "o"."CreationDate", "o"."ExpirationDate", "o"."Payload", "o"."Properties", "o"."RedemptionDate", "o"."ReferenceId", "o"."Status", "o"."Subject", "o"."Type", "o0"."Id", "o0"."ApplicationType", "o0"."ClientId", "o0"."ClientSecret", "o0"."ClientType", "o0"."ConcurrencyToken", "o0"."ConsentType", "o0"."DisplayName", "o0"."DisplayNames", "o0"."JsonWebKeySet", "o0"."Permissions", "o0"."PostLogoutRedirectUris", "o0"."Properties", "o0"."RedirectUris", "o0"."Requirements", "o0"."Settings", "o1"."Id", "o1"."ApplicationId", "o1"."ConcurrencyToken", "o1"."CreationDate", "o1"."Properties", "o1"."Scopes", "o1"."Status", "o1"."Subject", "o1"."Type"
FROM "OpenIddictTokens" AS "o"
LEFT JOIN "OpenIddictApplications" AS "o0" ON "o"."ApplicationId" = "o0"."Id"
LEFT JOIN "OpenIddictAuthorizations" AS "o1" ON "o"."AuthorizationId" = "o1"."Id"
WHERE "o"."ReferenceId" = @__identifier_0
LIMIT 1
01/14/24 11:57:40 007 [INFO] Executed DbCommand (0ms) [Parameters=[@p12='?' (Size = 36), @p0='?' (Size = 36), @p1='?' (Size = 36), @p2='?' (Size = 36), @p13='?' (Size = 36), @p3='?' (DbType = DateTime), @p4='?' (DbType = DateTime), @p5='?' (Size = 2321), @p6='?', @p7='?' (DbType = DateTime), @p8='?' (Size = 44), @p9='?' (Size = 5), @p10='?' (Size = 1), @p11='?' (Size = 18)], CommandType='Text', CommandTimeout='30']
UPDATE "OpenIddictTokens" SET "ApplicationId" = @p0, "AuthorizationId" = @p1, "ConcurrencyToken" = @p2, "CreationDate" = @p3, "ExpirationDate" = @p4, "Payload" = @p5, "Properties" = @p6, "RedemptionDate" = @p7, "ReferenceId" = @p8, "Status" = @p9, "Subject" = @p10, "Type" = @p11
WHERE "Id" = @p12 AND "ConcurrencyToken" = @p13
RETURNING 1;
01/14/24 11:57:40 019 [INFO] The authorization response was successfully returned to 'http://localhost:7000/' using the query response mode: {
"code": "[redacted]",
"state": "566rjklB0G3f93wV-XXGcK5ubqHGSE5bdzcbgHdtW1I",
"iss": "https://localhost:7296/"
}.
01/14/24 11:57:40 025 [INFO] Executed action Gt.IDP.Controllers.AuthorizationController.Authorize (Gt.IDP) in 85174.9359ms
01/14/24 11:57:40 026 [INFO] Executed endpoint 'Gt.IDP.Controllers.AuthorizationController.Authorize (Gt.IDP)'
01/14/24 11:57:40 029 [INFO] Request finished HTTP/2 GET https://localhost:7296/connect/authorize?client_id=core_api_client&redirect_uri=http%3A%2F%2Flocalhost%3A7000%2F&response_type=code&scope=openid%20gtapi&nonce=eBj-G8hrCf5lmlrzTGKvHxE2yJY9DcRPSnK692NMtXk&code_challenge=wHhnsm0VVO7t4gttG84I4eoRc6Q8cfIRBtyzwc5EP2E&code_challenge_method=S256&state=566rjklB0G3f93wV-XXGcK5ubqHGSE5bdzcbgHdtW1I&prompt= - - - 302 0 - 85262.1107ms
01/14/24 11:57:55 282 [INFO] Request starting HTTP/1.1 POST https://localhost:7296/connect/token application/x-www-form-urlencoded 208
01/14/24 11:57:55 283 [INFO] The request URI matched a server endpoint: Token.
01/14/24 11:57:55 292 [INFO] The token request was successfully extracted: {
"grant_type": "authorization_code",
"code": "[redacted]",
"code_verifier": "rVeZDmuBKIKJo1MuN54Yn5yFgCJJuSZMfzB8Fbg2thQ",
"redirect_uri": "http://localhost:7000/",
"client_id": "core_api_client"
}.
01/14/24 11:57:55 311 [INFO] Executed DbCommand (0ms) [Parameters=[@__identifier_0='?' (Size = 15)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationType", "o"."ClientId", "o"."ClientSecret", "o"."ClientType", "o"."ConcurrencyToken", "o"."ConsentType", "o"."DisplayName", "o"."DisplayNames", "o"."JsonWebKeySet", "o"."Permissions", "o"."PostLogoutRedirectUris", "o"."Properties", "o"."RedirectUris", "o"."Requirements", "o"."Settings"
FROM "OpenIddictApplications" AS "o"
WHERE "o"."ClientId" = @__identifier_0
LIMIT 1
01/14/24 11:57:55 344 [INFO] Executed DbCommand (0ms) [Parameters=[@__identifier_0='?' (Size = 44)], CommandType='Text', CommandTimeout='30']
SELECT "o"."Id", "o"."ApplicationId", "o"."AuthorizationId", "o"."ConcurrencyToken", "o"."CreationDate", "o"."ExpirationDate", "o"."Payload", "o"."Properties", "o"."RedemptionDate", "o"."ReferenceId", "o"."Status", "o"."Subject", "o"."Type", "o0"."Id", "o0"."ApplicationType", "o0"."ClientId", "o0"."ClientSecret", "o0"."ClientType", "o0"."ConcurrencyToken", "o0"."ConsentType", "o0"."DisplayName", "o0"."DisplayNames", "o0"."JsonWebKeySet", "o0"."Permissions", "o0"."PostLogoutRedirectUris", "o0"."Properties", "o0"."RedirectUris", "o0"."Requirements", "o0"."Settings", "o1"."Id", "o1"."ApplicationId", "o1"."ConcurrencyToken", "o1"."CreationDate", "o1"."Properties", "o1"."Scopes", "o1"."Status", "o1"."Subject", "o1"."Type"
FROM "OpenIddictTokens" AS "o"
LEFT JOIN "OpenIddictApplications" AS "o0" ON "o"."ApplicationId" = "o0"."Id"
LEFT JOIN "OpenIddictAuthorizations" AS "o1" ON "o"."AuthorizationId" = "o1"."Id"
WHERE "o"."ReferenceId" = @__identifier_0
LIMIT 1
01/14/24 11:57:55 418 [INFO] The response was successfully returned as a JSON document: {
"error": "invalid_grant",
"error_description": "The specified token is invalid.",
"error_uri": "https://documentation.openiddict.com/errors/ID2004"
}.
01/14/24 11:57:55 426 [INFO] Request finished HTTP/1.1 POST https://localhost:7296/connect/token application/x-www-form-urlencoded 208 - 400 161 application/json;charset=UTF-8 144.1770ms
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 27 (11 by maintainers)
I think Serilog uses a different configuration section by default.