1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Refresh tokens with Identity 8

Discussão em 'Angular' iniciado por Pierre Fraisse, Outubro 2, 2024.

  1. I am currently developing the authentication feature of an app using ASP.NET 8 with Identity 8 also and Angular in the front. I set up a basic http only login and registration, I have a nice login persistance but am having trouble understanding how refresh token works in this context.

    I have a refresh endpoint in my API but I can't access it, here's my frontend method for this :

    const headers = { 'Content-Type': 'application/json' }; console.log("Refreshing...");
    return this._httpClient.post(`${this.baseUrl}/refresh`, {}, { headers });}


    and it should be sent whenever I am not authenticated via an auth guard :

    if (accountService.currentUser()) {
    return of(true); // Allow access if user is authenticated
    } else {
    // Fetch the current authentication state
    return accountService.getAuthState().pipe(
    switchMap((authState) => {
    if (!authState.isAuthenticated) {
    // If not authenticated, try refreshing the token
    return accountService.refreshAccessToken().pipe(
    map(() => {
    // After refreshing, recheck the authentication state
    return true; // Token refresh was successful, allow access
    }),
    catchError(() => {
    // If refreshing fails, redirect to login
    router.navigate(['/account/login'], { queryParams: { returnUrl: state.url } });
    return of(false);
    })
    );
    } else {
    // If authenticated, allow access
    return of(true);
    }
    }),
    catchError(() => {
    // Handle any errors (e.g., network issues) and redirect to login
    router.navigate(['/account/login'], { queryParams: { returnUrl: state.url } });
    return of(false);
    })
    );
    }


    In my back-end I don't have a huge config, I just tried to give a really short lifespan to my token to see the refresh request get sent via the network tab but I don't:

    services.Configure<DataProtectionTokenProviderOptions>(options =>
    {
    options.TokenLifespan = TimeSpan.FromSeconds(3);
    });


    Am I missing something ? Do you need to see more of my code?

    I tried to add an empty {refreshToken: ''} to my request body but it doesn't work neither, tried to read the Mcrosoft doc...

    Continue reading...

Compartilhe esta Página