如何在ASP.NET Core 3.0中自动刷新令牌以进行API请求?

问题描述 投票:0回答:1

我有一个ASP NET Core 3.0应用程序,正在使用Azure Active Directory进行身份验证。硒以下的强化配置

services.AddAuthentication(options =>
{
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 })
.AddOpenIdConnect(options =>
{
    options.Authority = auth.Authority;
    options.ResponseType = OpenIdConnectResponseType.IdToken;
    options.SignedOutRedirectUri = auth.SignedOutRedirectUri;
    options.CallbackPath = auth.CallbackPath;
    options.ClientId = auth.ClientId;
})
.AddCookie(options =>
{
    options.AccessDeniedPath = "/Home/AccessDenied/";
    options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
}); 

cookie在闲置20分钟后更改页面时会自动重新生成,但是如果我调用API Controller,则不会发生这种情况。当我进行AJAX呼叫且令牌已过期时,我该怎么做以自动刷新令牌?我是否忘记在配置中添加某些内容?

api asp.net-core cookies token openid-connect
1个回答
0
投票

earlier post可能会给您一些想法-来自遵循相同概念的以前的MS技术堆栈。

您可能对SlidingExpiration选项感到幸运。传统上,由于2种不同的Authorize属性,这一直是个问题区域。

WIDER POINT

导致代码更简单的更简洁的体系结构是避免在同一服务器端组件中混合Web和API代码。

当然,这可能不在当前项目的范围内,但是对于下一个项目,您可能会考虑:

  • 验证令牌的ASP.Net Core Web API
  • 实现为单页应用程序的无cookie Web UI
  • Web静态内容托管
  • UI将令牌发送到API
  • UI在不涉及后端的情况下静默更新令牌

[如果有兴趣,请查看我的Azure AD示例:

© www.soinside.com 2019 - 2024. All rights reserved.