How to make Owin automatically use refresh token when access token expires

Viewed 974

I have an Owin client connected to IdentityServer 4, and am wondering how to get owin to request a new access_token using the refresh token. I can successfully get owin to swap the code given for an access_token, id_token and refresh_token with the following configuration:

public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "Cookie"
            });
            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                Authority = "http://localhost:5000",
                ClientId = "mywebsite",
                ClientSecret = "secret",
                RedirectUri = "https://localhost:5001/",
                ResponseType = "code",
                RequireHttpsMetadata = false,
                SaveTokens = true,
                UseTokenLifetime = true,
                SignInAsAuthenticationType = "Cookie",
                Scope = "openid profile email offline_access",
                RedeemCode = true,
                
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = n =>
                    {
                        Console.WriteLine(n);
                        return System.Threading.Tasks.Task.FromResult(0);
                    },
                    TokenResponseReceived = n =>
                    {
                        Console.WriteLine(n);
                        return System.Threading.Tasks.Task.FromResult(0);
                    }
                },
                
            });
        }

Firstly, where do I save these tokens to? I can access them all the SecurityTokenValidated callback - should they go into the claims? Database? Memory?

Secondly, I have on my IdentityServer client configuration the access_token lifespan set to 60s, identity_token set to 3600s, and refresh to 30 days (please note the access_token is only this short for testing purposes). So how can I configure Owin to recognize that the access_token has expired and that it needs to go back to identityserver with the refresh_token and get a new one. Answers with example code snippets would be appreciated as my knowledge on all this is very small.

Relevant Info: IS4 v3 .Net Framework v4.6 Client is set in IS to allow offline access

1 Answers
Related