首页 文章

IdentityServer 4- ASP.net webapi(非核心) - 缺少ApiResource

提问于
浏览
0

Given

我运行了一个identityserver v4 . Javascript客户端创建一个令牌并使用生成的accesstoken访问asp.core api .

在Asp Core Api中我有这个代码可行

services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = authority;
                options.ApiName = "myApi";
            });

在IdentityServer中,我将“ myApi ”定义为 ApiResource .

Problem

现在我想要包含另一个api,它是较旧的webapi并使用.net4.6.1编写 . 但所有对此api的调用都以 401 结束

对具有相同令牌的核心api使用相同的请求 .

看起来像这样的代码:

JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();
      var options = new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = identServer,
            RequiredScopes = new List<string>
            {
                "myApi"
            }
        };

        app.UseIdentityServerBearerTokenAuthentication(options);

但是正在运行核心项目的令牌在那里不起作用 . 我能看到的一件事是我无法在IdentityServerBearerTokenAuthenticationOptions中设置api名称 .

目前我正在使用IdentityServer 3 .AccessTokenValidation Nuget包 . 也许这与问题有关?

由于某些依赖性问题,我无法使用IdentityServer 4 .AccessTokenValidation . 例如,netcore身份验证构建器只有AddOptions

enter image description here

使用jwt.io我也对令牌进行了解决,并且可以看到myApi是 audscope 的一部分

我还尝试设置没有必要的范围仍然有401

前段时间我用身份服务器v3对它进行了测试 .

此外,我在日志中看不到任何验证错误 . 我记得错过api令牌验证的错误是什么?

我错过了什么?

Update 将validationmode设置为 endpoints 时,我在identityserver中遇到异常 .

fail:IdentityServer4.Validation.ApiSecretValidator [0]找不到具有该名称的API资源 . 中止失败:IdentityServer4.Endpoints.IntrospectionEndpoint [0] API未经授权调用内省 endpoints . 中止 .

不幸的是,它没有告诉我api是他预期的......正如前面提到的“myApi”被定义为ApiResource但是如何在非核心环境中设置api名称?

2 回答

  • 1

    原因是当我使用非核心api进行身份验证时,api资源需要有一个秘密,然后必须在api中设置api资源名称和秘密 .

    这里:

    在IdentityServer中

    new ApiResource("myApi", "My Test API")
    {   
      ApiSecrets = new List<Secret>()
      {
        new Secret("435tgsdgfdg".Sha256()) // This wasnt required when attaching a .net core api
      }
    }
    

    然后netfx webapi中的配置是:

    var options = new IdentityServerBearerTokenAuthenticationOptions
      {
        ClientId = "myApi",
        ClientSecret = "435tgsdgfdg",
        RequiredScopes = new List<string> { "myApi"},            
        Authority = identtityServerUri,
        DelayLoadMetadata = true
      };
    

    对于asp核心webapi,我既不需要在identityserver中设置秘密,也不需要在客户端设置clientid和client secret .

  • 0

    在对IDS4进行身份验证时可以使用 IdentityServer3.AccessTokenValidation ,不用担心 .

    试试这个:

    .net4.6.1 应用的 Startup.cs 中,在 IdentityServerBearerTokenAuthenticationOptions 中有一个属性 ValidationMode . 将其设置为 ValidationMode.Both 并再试一次 .

相关问题