首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Identityserver4中的函数IntrospectionClient总是返回Unauthorized

Identityserver4中的函数IntrospectionClient总是返回Unauthorized
EN

Stack Overflow用户
提问于 2018-05-25 17:42:48
回答 1查看 490关注 0票数 3

我想使用函数IntrospectionClient来验证身份服务器中的令牌,但它总是返回: Unauthorized

Identity server启动代码:

代码语言:javascript
复制
services.AddIdentityServer()
            .AddSigningCredential(myPrivatecert)
            .AddInMemoryApiResources(Config.GetResources())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetTestUsers())
            .AddValidationKey(myPubliccert)
            .AddJwtBearerClientAuthentication()
            .AddValidators();

我在配置文件中添加了api资源、客户端信息和测试用户

我的配置是:

代码语言:javascript
复制
public class Config
{
    //ApiResource
    public static IEnumerable<ApiResource> GetResources()
    {
        return new List<ApiResource>
        {
            new ApiResource("api")
        };
    }

    //Client
    public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
        {
            new Client()
            {
                ClientId = "site",
                AccessTokenLifetime = 180,
                RefreshTokenExpiration = TokenExpiration.Absolute,
                AbsoluteRefreshTokenLifetime = 1800,
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AlwaysSendClientClaims = true,
                AllowAccessTokensViaBrowser = true,
                RequireConsent = false,
                AllowedScopes =
                {
                    "api"
                },
                AccessTokenType = AccessTokenType.Jwt,
                AllowOfflineAccess = true
            },
        };
    }

    //TestUser
    public static List<TestUser> GetTestUsers()
    {
        return new List<TestUser>{
            new TestUser{
                SubjectId="1",
                Username="wyt",
                Password="123456",
                Claims = new List<Claim>()
                {
                    new Claim("wawa", "aoao"),
                }
            }
        };
    }
}

我的客户端启动是:

代码语言:javascript
复制
        services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                }).
                AddIdentityServerAuthentication(options =>
                {
                    options.Authority = "http://localhost:6001";
                    options.RequireHttpsMetadata = false;
                    options.ApiName = "api";
                });

获取令牌:

代码语言:javascript
复制
        //token
        var tokenClient = new TokenClient(dico.TokenEndpoint, "site", "secret");
        var tokenResponse = tokenClient.RequestResourceOwnerPasswordAsync("wyt", "123456").Result;

想要使用内省:

代码语言:javascript
复制
var introspectionClient = new IntrospectionClient(
            dico.IntrospectionEndpoint,
            "api",
            "secret");

        var vresponse = await introspectionClient.SendAsync
            (
                new IntrospectionRequest { Token = tokenResult.AccessToken }
            );

但是vresponse总是未经授权的,哪里错了?

EN

回答 1

Stack Overflow用户

发布于 2019-06-19 00:16:01

我遇到了同样的问题,文档不是很清楚。您需要在ApiResource中添加一个ApiSecret:

代码语言:javascript
复制
public static IEnumerable<ApiResource> GetResources()
{
    return new List<ApiResource>
    {
        new ApiResource("api") 
        {
            ApiSecrets = { new Secret("secret".Sha256()) }
        }
    };
}

然后调用内省端点,传递api名称和api密钥,就像您所做的那样:

代码语言:javascript
复制
var introspectionClient = new IntrospectionClient(
        dico.IntrospectionEndpoint,
        "api",
        "secret");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50526171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档