IdentityServer MVC令牌到期

Lia*_*iam 6 asp.net-mvc identityserver4

我是Identity Server的新手,我的理解中缺少一个关键概念。我正在使用MVC教程中的代码。

如果我用属性装饰Home控制器[Authorize]并访问我的网站,则会重定向到IdentityServer。然后,使用我的用户名和密码登录。然后,我使用一些自定义代码并进行身份验证。我取回一个AccessToken,然后可以访问Home控制器。

我的客户端设置如下:

  new Client {
                ClientId = "mvc",
                ClientName = "MVC Client",
                AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
                ClientSecrets = new List<Secret>{new Secret("secret".Sha256())},
                RequireConsent = false,
                AccessTokenLifetime = 1,

                // where to redirect to after login
                RedirectUris = new List<string>{"http://localhost:5002/signin-oidc"},

                // where to redirect to after logout
                PostLogoutRedirectUris = new List<string>{"http://localhost:5002"},

                AllowedScopes = new List<string>
                {
                    StandardScopes.OpenId.Name,
                    StandardScopes.Profile.Name,
                    StandardScopes.OfflineAccess.Name,
                }
            }
Run Code Online (Sandbox Code Playgroud)

我的访问令牌是

{
  "nbf": 1474697839,
  "exp": 1474697840,
  "iss": "http://localhost:5000",
  "aud": "http://localhost:5000/resources",
  "client_id": "mvc",
  "scope": [
    "openid",
    "profile"
  ],
  "sub": "26296",
  "auth_time": 1474697838,
  "idp": "local",
  "amr": [
    "pwd"
  ]
}
Run Code Online (Sandbox Code Playgroud)

当我将我设置AccessTokenLifetime为1时,我的令牌在发送来调用API等时将失效。但是,我仍然可以访问该网站。

使MVC网站确认我的令牌尚未过期的最佳方法是什么?这可能是刷新令牌起作用的地方。

注意AccessTokenLifetime设置为1仅用于测试,所以我可以快速测试的东西。

Jes*_*er1 -1

也许像这样?

var user = HttpContext.Current.User.Identity;
                    if (!user.IsAuthenticated)
Run Code Online (Sandbox Code Playgroud)