JonPSmith / AuthPermissions.AspNetCore

This library provides extra authorization and multi-tenant features to an ASP.NET Core application.
https://www.thereformedprogrammer.net/finally-a-library-that-improves-role-authorization-in-asp-net-core/
MIT License
764 stars 155 forks source link

How exactly does the refresh token work? #113

Open akema-trebla opened 2 weeks ago

akema-trebla commented 2 weeks ago

Does the token refresh only work before token expiry or does it still work after token expiry?

Also, I get the following error when I try to run Example 2 to verify the above:

Unable to validate issuer. The 'issuer' parameter is null or whitespace.

Any pointers? Thanks.

JonPSmith commented 2 days ago

Hi @akema-trebla,

Have a look at a video I created about AuthP's JWT Token With Refresh feature. This section of the video shows how you can log in via Swagger in example 2.

akema-trebla commented 1 day ago

Hi @JonPSmith

Thanks for your resources and I've watched them and the video explains it as I expect it to work. ie. even after the token has expired, the refresh token can be used to generate a new token and refresh token.

However, that does not happen even in Example 2.

Here's the error from Example 2 Microsoft.IdentityModel.Tokens.SecurityTokenInvalidIssuerException: IDX10211: Unable to validate issuer. The 'issuer' parameter is null or whitespace at Microsoft.IdentityModel.Tokens.Validators.ValidateIssuerAsync(String issuer, SecurityToken securityToken, TokenValidationParameters validationParameters, BaseConfiguration configuration) at Microsoft.IdentityModel.Tokens.Validators.ValidateIssuer(String issuer, SecurityToken securityToken, TokenValidationParameters validationParameters, BaseConfiguration configuration) at Microsoft.IdentityModel.Tokens.Validators.ValidateIssuer(String issuer, SecurityToken securityToken, TokenValidationParameters validationParameters) at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateIssuer(String issuer, JwtSecurityToken jwtToken, TokenValidationParameters validationParameters) at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateTokenPayload(JwtSecurityToken jwtToken, TokenValidationParameters validationParameters, BaseConfiguration configuration) at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateJWS(String token, TokenValidationParameters validationParameters, BaseConfiguration currentConfiguration, SecurityToken& signatureValidatedToken, ExceptionDispatchInfo& exceptionThrown) --- End of stack trace from previous location --- at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, JwtSecurityToken outerToken, TokenValidationParameters validationParameters, SecurityToken& signatureValidatedToken) at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken) at AuthPermissions.AspNetCore.JwtTokenCode.TokenBuilder.GetPrincipalFromExpiredToken(String token) in /Users/akema_trebla/Downloads/AuthPermissions.AspNetCore-main 2/AuthPermissions.AspNetCore/JwtTokenCode/TokenBuilder.cs:line 203 at AuthPermissions.AspNetCore.JwtTokenCode.TokenBuilder.RefreshTokenUsingRefreshTokenAsync(TokenAndRefreshToken tokenAndRefresh) in /Users/akema_trebla/Downloads/AuthPermissions.AspNetCore-main 2/AuthPermissions.AspNetCore/JwtTokenCode/TokenBuilder.cs:line 93 at Example2.WebApiWithToken.IndividualAccounts.Controllers.AuthenticateController.RefreshAuthentication(TokenAndRefreshToken tokenAndRefresh) in /Users/akema_trebla/Downloads/AuthPermissions.AspNetCore-main 2/Example2.WebApiWithToken.IndividualAccounts/Controllers/AuthenticateController.cs:line 107 at lambda_method363(Closure, Object) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gAwaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Example2.WebApiWithToken.IndividualAccounts.ClaimsChangeCode.AddEmailClaimMiddleware.<>c.<b1_0>d.MoveNext() in /Users/akema_trebla/Downloads/AuthPermissions.AspNetCore-main 2/Example2.WebApiWithToken.IndividualAccounts/ClaimsChangeCode/AddEmailClaimMiddleware.cs:line 29 --- End of stack trace from previous location --- at Example2.WebApiWithToken.IndividualAccounts.ClaimsChangeCode.UpdateRoleClaimMiddleware.<>c.<b__1_0>d.MoveNext() in /Users/akema_trebla/Downloads/AuthPermissions.AspNetCore-main 2/Example2.WebApiWithToken.IndividualAccounts/ClaimsChangeCode/UpdateRoleClaimMiddleware.cs:line 36 --- End of stack trace from previous location --- at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) 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)