访问令牌过期时如何使Owin自动使用刷新令牌

时间:2020-07-22 05:16:35

标签: asp.net owin identityserver4 openid-connect

我有一个连接到IdentityServer 4的Owin客户端,并且想知道如何使用刷新令牌来获取OWIN以请求新的access_token。我可以通过以下配置成功获得owin来交换为access_token,id_token和refresh_token给出的代码:

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);
                    }
                },
                
            });
        }

首先,我将这些令牌保存到哪里?我可以访问所有的SecurityTokenValidated回调-它们是否应包含在声明中?数据库?记忆吗?

其次,我在IdentityServer客户端配置上将access_token寿命设置为60s,identity_token设置为3600s,并刷新为30天(请注意,access_token仅是出于测试目的的缩写)。因此,我该如何配置Owin以识别access_token已过期,并且需要使用refresh_token返回到identityserver并获取一个新的。由于我对所有这些知识的了解非常小,因此感谢示例代码片段的答案。

相关信息: IS4 v3 .Net Framework v4.6 在IS中将客户端设置为允许离线访问

1 个答案:

答案 0 :(得分:0)

看看这篇文章:

否则,AddOpenIdConnect(..)处理程序中没有逻辑来处理刷新令牌的更新。我认为您的应用程序可以刷新它们。如果您将刷新令牌保存在安全的地方,那么用代码刷新它们并不难。

看到这个问题How to use 'refresh_token' in IdentityServer 4?