IdentityServer4刷新令牌为空

本文关键字:令牌 刷新 IdentityServer4 | 更新日期: 2023-09-27 18:18:05

我已经为这个问题挠头好几天了。我在IdentityServer4中遇到一个问题,其中令牌响应不包含刷新令牌。关于获取访问令牌,我已经工作得很好-这只是刷新令牌,我似乎无法弄清楚。

在我的项目。在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。客户,配置。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!?
    }
}

我读了不少文章,但我找不到一个地方,他们只关注刷新令牌。它们似乎都只关注返回访问令牌的基本代码。

有人能告诉我我错过了什么,也许显示/指出我在正确的方向?任何帮助将非常感激!

IdentityServer4刷新令牌为空

在请求令牌时需要包含offline_access作用域。

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