I've been scratching my head on this problem for quite a few days already. I have a problem with IdentityServer4 where the token response does not contain a refresh token. The code I have already works fine regarding getting the access token - it's just the refresh token that I cannot seem to figure out.
In my project.json file, I have added the following entries:
"IdentityServer4": "1.0.0-rc3",
"IdentityServer4.AccessTokenValidation": "1.0.1-rc3"
My Startup.cs file contains the following:
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();
}
You may have noticed that I am using Config.Clients, Config.Scope, and Config.Users. Here is the code for those (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")
}
}
};
}
}
}
And I am calling this IdentityController.cs in order to get the Token:
[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!?
}
}
I have read quite a few articles, but no where could I find a place where they focus on the Refresh Token alone. They all seem to focus just on basic code that returns the Access Token.
Can someone tell me what I am missing, and maybe show/point me in the right direction? Any help will be greatly appreciated!
You need to include the offline_access scope when requesting the token.
But the client credentials flow does not have support for refresh tokens - so this will not work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With