gpt4 book ai didi

asp.net - 如何让 Owin 在访问 token 过期时自动使用刷新 token

转载 作者:行者123 更新时间:2023-12-04 17:26:21 24 4
gpt4 key购买 nike

我有一个 Owin 客户端连接到 IdentityServer 4,我想知道如何让 owin 使用刷新 token 请求新的 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);
}
},

});
}
首先,我应该将这些 token 保存到哪里?我可以访问他们所有的 SecurityTokenValidated 回调——他们应该进入声明吗?数据库?内存?
其次,我在我的 IdentityServer 客户端配置中将 access_token 生命周期设置为 60 秒,将 identity_token 设置为 3600 秒,并刷新为 30 天(请注意,access_token 只是为了测试目的而已)。那么我如何配置 Owin 以识别 access_token 已过期,并且它需要使用 refresh_token 返回到 identityserver 并获取一个新 token 。示例代码片段的答案将不胜感激,因为我对这一切的了解非常少。
相关资料:
IS4 v3
.Net 框架 v4.6
客户端在IS中设置为允许离线访问

最佳答案

看看这篇文章:

  • Automatic Token Management for ASP.NET Core and Worker Services 1.0

  • 除此之外,AddOpenIdConnect(..) 处理程序中没有处理刷新 token 更新的逻辑。我认为这取决于您的应用程序来刷新它们。如果您将刷新 token 保存在安全的地方,则在代码中刷新它们并不难。
    看到这个问题 How to use 'refresh_token' in IdentityServer 4?

    关于asp.net - 如何让 Owin 在访问 token 过期时自动使用刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63027373/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com