1

我已經在這個問題上摸索了好幾天了。我在IdentityServer4中存在令牌響應不包含刷新令牌的問題。我已經正確地使用了代碼來獲取訪問令牌 - 這只是我無法想象的刷新令牌。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(); 
} 

您可能已經注意到,我使用的配置.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!? 
    } 
} 

我看了好幾篇文章,但沒有我在哪裏可以找到一個地方,他們只關注刷新令牌。他們似乎都只關注返回訪問令牌的基本代碼。

有人可以告訴我我失蹤了什麼,也許在正確的方向顯示/指向我?任何幫助將不勝感激!

回答

2

請求令牌時,您需要包含offline_access範圍。

但客戶端憑據流不支持刷新令牌 - 所以這是行不通的。

+0

那麼我可以修改任何東西來實現它嗎?或者目前不可能獲得刷新令牌? –

+1

也許從描述你實際上想要做的事開始 - 而不是向我們投擲代碼 – leastprivilege

+0

不需要擔心,我不會再使用刷新標記,因爲目前看起來不可能 –