【问题标题】:IdentityServer MVC Token ExpirationIdentityServer MVC 令牌过期
【发布时间】:2017-02-02 01:11:26
【问题描述】:

我是身份服务器的新手,我的理解中缺少一个关键概念。 我正在使用来自MVC tutorial 的代码。

如果我用属性 [Authorize] 装饰我的 Home 控制器并访问我的网站,我会重定向到 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,
                }
            }

我的访问令牌是

{
  "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"
  ]
}

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

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

注意 AccessTokenLifetime 设置为 1 仅用于测试,以便我可以快速测试。

【问题讨论】:

    标签: asp.net-mvc identityserver4


    【解决方案1】:

    也许是这样的?

    var user = HttpContext.Current.User.Identity;
                        if (!user.IsAuthenticated)
    

    【讨论】:

      【解决方案2】:

      您需要设置用户身份验证生命周期以匹配您的访问令牌的生命周期。如果使用 OpenIdConnect,您可以使用以下代码:

      .AddOpenIdConnect(option =>
      {
      ...
      
      option.Events.OnTicketReceived = async context =>
      {
          // Set the expiry time to match the token
          if (context?.Properties?.Items != null && context.Properties.Items.TryGetValue(".Token.expires_at", out var expiryDateTimeString))
          {
              if(DateTime.TryParse(expiryDateTimeString, out var expiryDateTime))
              {
                  context.Properties.ExpiresUtc = expiryDateTime.ToUniversalTime();
              }
          }
      };
      });
      

      我假设您正在使用 cookie 身份验证?如果是这样,您可能必须关闭滑动到期。滑动过期将在处理超过过期窗口一半的请求时自动刷新 cookie。但是,访问令牌不会作为其中的一部分刷新。因此,您应该让用户身份验证生命周期一直运行到结束,届时它将过期,并且将使用刷新令牌自动检索新的访问令牌。

      .AddCookie(options =>
                  {
                      // Do not re-issue a new cookie with a new expiration time.
                      // We need to let it expire to ensure we get a fresh JWT within the token lifetime.
                      options.SlidingExpiration = false;
                  })
      

      【讨论】:

        猜你喜欢
        • 2016-11-13
        • 1970-01-01
        • 2016-10-01
        • 2016-09-29
        • 1970-01-01
        • 2015-03-12
        • 2013-06-28
        • 2016-10-07
        • 2017-07-15
        相关资源
        最近更新 更多