【问题标题】:IdentityServer4 Refresh Token Is NullIdentityServer4 刷新令牌为空
【发布时间】:2017-03-30 13:06:48
【问题描述】:

我已经在这个问题上摸不着头脑好几天了。我对 IdentityServer4 有问题,其中令牌响应不包含刷新令牌。关于获取访问令牌,我已经可以正常工作的代码 - 这只是我似乎无法弄清楚的刷新令牌。

在我的 project.json 文件中,我添加了以下条目:

"IdentityServer4": "1.0.0-rc3",
"IdentityServer4.AccessTokenValidation": "1.0.1-rc3"

我的 Startup.cs 文件包含以下内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentityServer()
        .AddInMemoryClients(Config.Clients.Get())
        .AddInMemoryScopes(Config.Scopes.Get())
        .AddInMemoryUsers(Config.Users.Get())
        .AddTemporarySigningCredential();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseIdentityServer();
}

您可能已经注意到我正在使用 Config.Clients、Config.Scope 和 Config.Users。这是那些(Config.cs)的代码:

public class Config
{
    internal class Clients
    {
        public static IEnumerable<Client> Get()
        {
            return new List<Client> {
                new Client
                {
                    ClientId = "client",
                    ClientName = "Authentication Client",
                    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
                    AccessTokenLifetime = 1800,
                    RefreshTokenExpiration = TokenExpiration.Absolute,
                    AbsoluteRefreshTokenLifetime = 1800,
                    ClientSecrets = new List<Secret> {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = new List<string>
                    {
                        "api",
                        StandardScopes.OfflineAccess.Name, //For refresh tokens

                    }
                }
            };
        }
    }

    internal class Scopes
    {
        public static IEnumerable<Scope> Get()
        {
            return new List<Scope>
            {
                StandardScopes.OfflineAccess, //For refresh tokens
                new Scope {
                    Name = "api",
                    DisplayName = "API",
                    Description = "API scope",
                    Type = ScopeType.Resource,
                    Claims = new List<ScopeClaim> {
                        new ScopeClaim(JwtClaimTypes.Role)
                    },
                    ScopeSecrets =  new List<Secret> {
                        new Secret("secret".Sha256())
                    }
                }
            };
        }
    }

    internal class Users
    {
        public static List<InMemoryUser> Get()
        {
            return new List<InMemoryUser>
            {
                new InMemoryUser {
                    Subject = "5BE86359-073C-434B-AD2D-A3932222DABE",
                    Username = "admin",
                    Password = "admin",
                    Claims = new List<Claim> {
                        new Claim(JwtClaimTypes.Role, "admin")
                    }
                }
            };
        }
    }
}

我调用这个 IdentityController.cs 是为了获取令牌:

[Route("api/[controller]")]
public class IdentityController : ControllerBase
{
    [HttpPost("GetToken")]
    public string GetToken()
    {
        DiscoveryResponse dr = new DiscoveryClient("http://localhost:5000").GetAsync().Result;
        TokenClient tokenClient = new TokenClient(dr.TokenEndpoint, "client", "secret", AuthenticationStyle.BasicAuthentication);
        TokenResponse tokenResponse = tokenClient.RequestClientCredentialsAsync("api").Result;

        return tokenResponse.AccessToken; //"tokenResponse" does not contain the refresh token!?
    }
}

我已经阅读了很多文章,但是我在哪里找不到他们只关注刷新令牌的地方。他们似乎都只关注返回访问令牌的基本代码。

有人可以告诉我我缺少什么,并可能向我展示/指出正确的方向吗?任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    您需要在请求令牌时包含 offline_access 范围。

    但客户端凭据流不支持刷新令牌 - 所以这不起作用。

    【讨论】:

    • 那么我可以修改任何东西来实现它吗?还是目前无法获得刷新令牌?
    • 也许从描述你真正想要做的事情开始——而不是向我们扔代码
    • 不用担心,我不会再使用刷新令牌了,因为目前看来不可能
    猜你喜欢
    • 2018-07-07
    • 2019-03-23
    • 2020-09-12
    • 2019-12-23
    • 2018-08-09
    • 2020-02-23
    • 2017-12-25
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多