1

I am using React as client and Web API core for back end interaction. For Authentication we are using Token based authentication using AspNet.Security.OpenIdConnect.Server (ASOS).

I have to implement refresh token scenario where on expiration of access token we use refresh token (returned by ASOS) to get new access Token.

I know one way to achieve by calling method on client is in AXIOS interceptor like below.

httpPromise.interceptors.response.use(undefined, err => {
  const { config, response: { status } } = err;
  const originalRequest = config;

  if (status === 401) {

    var refresh_Token = JSON.parse(window.localStorage.getItem('refreshToken'));
    fetch(globalConstant.WEB_API_BASE_PATH + "authtoken,
      {
        method: "POST",
        headers: new Headers({
          'Content-Type': 'application/json',
        })
      },
      data:{grant-type:"refresh_Token",refresh_token:"refresh Token ....."
    )
    ....other logic to set new access token and make call again to existing 
    request.
   }
})

I want to done it in WEB API Core side, so that in middle ware or in authentication pipeline it detects access token expiration and return new access token. The glimpse of WEB API code is like below.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
.... some code
    serives.AddAuthentication(o =>
     {                    
        o.DefaultAuthenticateScheme = OAuthValidationDefaults.AuthenticationScheme;                    
    })
    serives.AddOAuthValidation()           
    serives.AddOpenIdConnectServer(options =>
    {
        options.ProviderType = typeof(AuthorizationProvider);
        options.Provider = new AuthorizationProvider(new SecurityService());
        options.TokenEndpointPath = "/authtoken";
        options.UserinfoEndpointPath = "/userInfo";
        options.AllowInsecureHttp = true;
        options.ApplicationCanDisplayErrors = true;
    });
..some code
}

The links i followed How to handle expired access token in asp.net core using refresh token with OpenId Connect and https://github.com/mderriey/aspnet-core-token-renewal/blob/master/src/MvcClient/Startup.cs

Sandeep Rasgotra
  • 582
  • 1
  • 7
  • 18
  • Possible duplicate of [How to get token details like access token,access token expiry time and refresh token etc. on OnAuthorization() method](https://stackoverflow.com/questions/56327048/how-to-get-token-details-like-access-token-access-token-expiry-time-and-refresh) – Edward May 28 '19 at 05:35
  • yes this link could be one of the way to achieve this , but if somebody have another way he/she can share. – Sandeep Rasgotra May 28 '19 at 05:56

0 Answers0