IdentityServer4-令牌刷新不';我第一次似乎不工作

IdentityServer4-令牌刷新不';我第一次似乎不工作,identityserver4,session-cookies,access-token,Identityserver4,Session Cookies,Access Token,我有一个ASP.NET核心MVC应用程序,它使用IdentityServer 4作为身份提供者。为了帮助调试,我将IdentityServer cookie设置为5分钟后过期,并将MVC应用程序中的会话超时设置为2分钟 在MVC应用程序的中间件管道中,我在app.UseAuthentication()之后有一个“RefreshTokenMiddleware”使用以下代码: if (!httpContext.User.Identity.IsAuthenticated || httpContext.

我有一个ASP.NET核心MVC应用程序,它使用IdentityServer 4作为身份提供者。为了帮助调试,我将IdentityServer cookie设置为5分钟后过期,并将MVC应用程序中的会话超时设置为2分钟

在MVC应用程序的中间件管道中,我在
app.UseAuthentication()之后有一个“RefreshTokenMiddleware”使用以下代码:

if (!httpContext.User.Identity.IsAuthenticated || httpContext.Request.Path.Value.Contains("logout"))
{
    await this.next(httpContext);
    return;
}

// If we're more than a minute away from the access token expiring, there's nothing to do here.
var expires = await httpContext.GetTokenAsync(CookieAuthenticationDefaults.AuthenticationScheme, "expires_at");
if (DateTimeOffset.UtcNow < DateTimeOffset.Parse(expires).AddMinutes(-1))
{
    await this.next(httpContext);
    return;
}

// Otherwise get a new refreshToken and update our session
var refreshToken = await httpContext.GetTokenAsync(CookieAuthenticationDefaults.AuthenticationScheme, "refresh_token");
var response = await tokenClient.RequestRefreshTokenAsync(refreshToken).ConfigureAwait(false);

var authentication = await httpContext.AuthenticateAsync();
authentication.Properties.UpdateTokenValue("expires_at", DateTimeOffset.UtcNow.AddSeconds(response.ExpiresIn).ToString("o", CultureInfo.InvariantCulture));
authentication.Properties.UpdateTokenValue("refresh_token", response.RefreshToken);
authentication.Properties.UpdateTokenValue("access_token", response.AccessToken);

await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, authentication.Principal, authentication.Properties);

await this.next(httpContext);
似乎需要在原始会话中通过管道进行后续传输,以接受在90秒时创建的新会话。至少我是这样理解场景2失败了,但是场景3通过了

这是预期的行为吗?我怀疑我遗漏了什么,但我在文档或我看到的刷新令牌的其他代码示例中找不到它

Scenario 1 - First refresh at 135 seconds

    0        60      120
    |--------|--------|
                        X
Expected Result - Unauthenticated at 135 seconds
Actual Result - Unauthenticated at 135 seconds

Scenario 2 - First refresh at 90 seconds (creating a new session), Second refresh at 135 seconds

    0        60  90  120  150      210
    |--------|--------|
                  X     
                  |--------|--------|
                        X
    
Expected Result - To be authenticated at 135 seconds as the refresh at 90 seconds creates new session    
Actual Result - Unauthenticated at 135 seconds

Scenario 3 - First refresh at 90 seconds (creating a new session), Second refresh at 105 seconds and third refresh at 135 seconds

    0        60  90  120  150      210
    |--------|--------|
                  X     
                  |--------|--------|
                    X    X
             
Expected Result - To be authenticated at 135 seconds as the refresh at 90 seconds creates new session
Actual Result - Authenticated at 135 seconds