3

私はすでにかなりの数日間、この問題について頭を悩ませてきました。トークン応答に更新トークンが含まれていない 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!?
    }
}

かなりの数の記事を読みましたが、更新トークンだけに焦点を当てている場所をどこで見つけることができませんでした. それらはすべて、アクセス トークンを返す基本的なコードだけに焦点を当てているようです。

誰かが私に欠けているものを教えてくれますか?おそらく正しい方向に私を示したり指摘したりできますか? どんな助けでも大歓迎です!

4

1 に答える 1

7

トークンを要求するときは、offline_access スコープを含める必要があります。

ただし、クライアント資格情報フローは更新トークンをサポートしていないため、これは機能しません。

于 2016-11-17T19:35:09.620 に答える