【问题标题】:IdentityServer4 refresh token invalid grantIdentityServer4 刷新令牌无效授权
【发布时间】:2017-12-25 03:48:48
【问题描述】:

我在 IdentityServer4 中请求新的刷新令牌时遇到了一些问题。身份验证后的某个时间,我从我的 API 收到未经授权的响应,好的,但是当我尝试请求新的刷新令牌时,我从服务器获得了一个 invalid_grant。我确保设置了offline_access,但仍然遇到问题。这是我的代码:

Server Config.cs 中的我的客户端

new Client
            {
                ClientId = "myclientId",
                ClientName = "MyClient",
                AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

                RequireConsent = false,

                ClientSecrets =
                {
                    new Secret("mySecret".Sha256())
                },

                RedirectUris = { "http://localhost:5002/signin-oidc" },
                PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

                AllowOfflineAccess = true,

                UpdateAccessTokenClaimsOnRefresh = true,

                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.OfflineAccess,
                    ...
                }
            }

来自 MVCclient 的我的 Startup.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationScheme = "Cookies",
            AccessDeniedPath = "/Home/Error403"
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
        {
            AuthenticationScheme = "oidc",
            SignInScheme = "Cookies",

            Authority = Configuration["Identity.Url"],
            RequireHttpsMetadata = false,

            ClientId = Configuration["ClientId"],
            ClientSecret = Configuration["ClientSecret"],

            ResponseType = "code id_token",

            Scope = { "openid profile offline_access" },

            GetClaimsFromUserInfoEndpoint = true,
            SaveTokens = true,

        });

我在这里得到了 invalid_grant

var disco = await DiscoveryClient.GetAsync(Configuration["Identity.Url"]);
        if (disco.IsError) throw new Exception(disco.Error);

        var tokenClient = new TokenClient(disco.TokenEndpoint, Configuration["ClientId"],
            Configuration["ClientSecret"]);
        var rt = await HttpContext.Authentication.GetTokenAsync("refresh_token");
        var tokenResult = await tokenClient.RequestRefreshTokenAsync(rt);

tokenResult 被分配了 invalid_grant。 我错过了什么吗?

【问题讨论】:

    标签: c# asp.net-core identityserver4 openid refresh-token


    【解决方案1】:

    尝试在 mvc 客户端配置中更改此行

    Scope = { "openid", "profile", "offline_access" }
    

    注意每个范围的引号

    【讨论】:

    • 对我来说,其他一切看起来都不错...检查身份验证服务器上的日志。这为我解决了许多问题。如果您使用的是 Visual Studio,请尝试将您的身份验证服务器作为控制台应用程序而不是 IIS Express 运行...希望对您有所帮助!
    • 当我的访问/刷新令牌仍然有效时,RequestRefreshTokenAsync() 工作正常。但是当它过期时,我得到了 invalid_grant。是这样吗?如果是这样,那我必须在到期前续订,对吗?
    • 如果您的访问令牌已过期或在过期之前,您可以请求 RequestRefreshTokenAsync(),但要获取新的访问令牌,您的刷新令牌应该是有效的(未过期)。但是,如果您的刷新令牌也已过期,那么您将收到错误消息——您可以检查身份验证服务器,它还会记录 fail 以及它无法验证刷新令牌。因此,刷新令牌到期会给您带来错误。确保在刷新令牌过期之前更新令牌(您可以使用滑动刷新令牌)
    • 我还是有同样的问题。一段时间后,我请求的所有有效或过期的访问令牌都会变成“找不到签名密钥”。我的请求得到了 invalid_grant。在 localhost 这永远不会发生。可以是我的主机服务器中的东西吗?可以是 SecurityTokenSignatureKey 吗?可以是 SSL 吗?
    • 伙计,我找到了问题!它是我的身份服务器中的 AddTemporarySigningCredential()。在我的池回收后,我所有的令牌都变得无效。谢谢!
    【解决方案2】:

    我发现当 IdentityServer 被 IIS 置于睡眠状态时会发生这种情况。我们有一个没有“保持活动/唤醒”策略的开发服务器,所以如果离开一段时间(我认为是 20 分钟),网站就会进入睡眠状态......再次使用时,这会破坏任何使用的尝试刷新令牌,但奇怪的是,我没有声称我的刷新令牌无效,而是在响应中收到“无效的 Grand Type”错误。

    【讨论】:

      【解决方案3】:

      我刚刚遇到了同样的错误。我注意到当我尝试在短时间内刷新访问令牌时发生错误,例如。每秒两次。 解决方案是将客户端的数据库(表dbo.Clients)中的RefreshTokenUssage参数增加到10(原始值为1)。

      【讨论】:

        猜你喜欢
        • 2019-10-17
        • 1970-01-01
        • 2013-11-19
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 2020-04-24
        • 1970-01-01
        • 2019-03-23
        相关资源
        最近更新 更多