Identityserver4 版本为4.0.0的文档中的示例中的Identity Server作用域无效

Identityserver4 版本为4.0.0的文档中的示例中的Identity Server作用域无效,identityserver4,Identityserver4,我正在使用IdentityServer4,遵循 但是,在请求具有客户端凭据的令牌时,使用IdentityModel的客户端中出现invalid_scope错误 我可能错过了一些步骤,但我已经复习了好几次了 奇怪的是,identity server端点显示以下日志: Invalid scopes requested, {"ClientId": "client", "ClientName": null, "GrantType&q

我正在使用IdentityServer4,遵循

但是,在请求具有客户端凭据的令牌时,使用
IdentityModel
的客户端中出现
invalid_scope
错误

我可能错过了一些步骤,但我已经复习了好几次了

奇怪的是,identity server端点显示以下日志:

Invalid scopes requested, {"ClientId": "client", "ClientName": null, "GrantType": "client_credentials", "Scopes": null, "AuthorizationCode": null, "RefreshToken": null, "UserName": null, "AuthenticationContextReferenceClasses": null, "Tenant": null, "IdP": null, "Raw": {"grant_type": "client_credentials", "scope": "api1", "client_id": "client", "client_secret": "***REDACTED***"}, "$type": "TokenRequestValidationLog"}
Scopes
null
并且以后的
scope
具有
api1
值,这不是很奇怪吗

我正在使用内存中的值

public static class Config
{
    public static IEnumerable<IdentityResource> Ids =>
        new IdentityResource[]
        { 
            new IdentityResources.OpenId()
        };

    public static IEnumerable<ApiResource> Apis =>
        new List<ApiResource>
        {
            new ApiResource("api1", "My Api")
        };
    
    public static IEnumerable<Client> Clients =>
        new List<Client>
        {
            new Client
            {
                ClientId = "client",
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AllowedScopes = { "api1" }
            }
        };
    
}
我可以看到众所周知的配置,尽管它没有提到api1是受支持的作用域

这是客户

var client = new HttpClient();
var discovery = 
    await client.GetDiscoveryDocumentAsync("https://localhost:5001");
if (discovery.IsError)
{
    await Console.Out.WriteLineAsync("Discovery error");
    return;
}

// request token
var clientCredentialsTokenRequest =
    new ClientCredentialsTokenRequest
    {
        Address = discovery.TokenEndpoint,
        ClientId = "client",
        ClientSecret = "secret",
        Scope = "api1"
    };

var tokenResponse = 
    await client.RequestClientCredentialsTokenAsync(clientCredentialsTokenRequest);
我是否遗漏了一些最基本的样品工作


更新1:

好的,我已经将Identity Server降级为3.1.3,它可以正常工作。 对于Identity Server 4.0.0版本,某些内容必须已更改。将在那里进行调查。

发现了一个为我指明正确方向的方法。通过使用ApiScopes替换ApiResources修复了此问题:

public static IEnumerable<ApiScope> Apis =>
    new List<ApiScope>
    {
        new ApiScope("api1", "My Api")
    };
我想文档还没有更新


在尝试访问受保护的Api时,我仍然会遇到未经授权的问题,但这是另外一回事。

我知道为时已晚,但我想再向您展示一个解决方案。 您的ApiResources应该与您的APIscope相匹配,因为它们将在以后进行匹配。这就是为什么删除额外的内存资源解决了您的问题。因为你关闭了“匹配功能”。希望它能帮助别人

Startup.cs

services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryApiScopes(Config.ApiScopes)
            .AddInMemoryIdentityResources(Config.IdentityResources)
            .AddInMemoryApiResources(Config.ApiResources)
            .AddInMemoryClients(Config.Clients);
public static class Config
{
    public static IEnumerable<IdentityResource> IdentityResources =>
        new IdentityResource[]
        {
            new IdentityResources.OpenId()
        };

    public static IEnumerable<ApiScope> ApiScopes =>
        new ApiScope[]
        {
            new ApiScope("SignalR", "SignalR Chat")
        };

    public static IEnumerable<ApiResource> ApiResources => 
        new List<ApiResource>
        {
            new ApiResource("SignalR", "SignalR Chat")
        };

    public static IEnumerable<Client> Clients =>
        new Client[]
        {
            new Client
            {
            ClientId = "client",

            // no interactive user, use the clientid/secret for authentication
            AllowedGrantTypes = GrantTypes.ClientCredentials,

            // secret for authentication
            ClientSecrets =
            {
                new Secret("secret".Sha256())
            },

            // scopes that client has access to
            AllowedScopes = { "SignalR" }
            }
        };
}
Config.cs

services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryApiScopes(Config.ApiScopes)
            .AddInMemoryIdentityResources(Config.IdentityResources)
            .AddInMemoryApiResources(Config.ApiResources)
            .AddInMemoryClients(Config.Clients);
public static class Config
{
    public static IEnumerable<IdentityResource> IdentityResources =>
        new IdentityResource[]
        {
            new IdentityResources.OpenId()
        };

    public static IEnumerable<ApiScope> ApiScopes =>
        new ApiScope[]
        {
            new ApiScope("SignalR", "SignalR Chat")
        };

    public static IEnumerable<ApiResource> ApiResources => 
        new List<ApiResource>
        {
            new ApiResource("SignalR", "SignalR Chat")
        };

    public static IEnumerable<Client> Clients =>
        new Client[]
        {
            new Client
            {
            ClientId = "client",

            // no interactive user, use the clientid/secret for authentication
            AllowedGrantTypes = GrantTypes.ClientCredentials,

            // secret for authentication
            ClientSecrets =
            {
                new Secret("secret".Sha256())
            },

            // scopes that client has access to
            AllowedScopes = { "SignalR" }
            }
        };
}
公共静态类配置
{
公共静态IEnumerable IdentityResources=>
新标识资源[]
{
新的IdentityResources.OpenId()
};
公共静态IEnumerable ApiScopes=>
新安培镜[]
{
新ApiScope(“信号器”、“信号器聊天”)
};
公共静态IEnumerable ApiResources=>
新名单
{
新资源(“信号器”、“信号器聊天”)
};
公共静态IEnumerable客户端=>
新客户[]
{
新客户
{
ClientId=“客户端”,
//无交互用户,请使用clientid/secret进行身份验证
AllowedGrantTypes=GrantTypes.ClientCredentials,
//身份验证的秘密
客户秘密=
{
新密码(“Secret.Sha256())
},
//客户端有权访问的作用域
AllowedScopes={“信号器”}
}
};
}

此版本在的网站上发布。它提到了。还有一个链接指向。