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

时间:2018-01-23 13:22:53

标签: asp.net asp.net-web-api identityserver4 identityserver3

鉴于

我正在运行身份服务器v4。 Javascript客户端创建一个令牌并使用生成的accesstoken访问asp.core api。

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

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

在IdentityServer中,我定义了" myApi "作为 ApiResource

问题

现在我想要包含另一个更旧的webapi并用.net4.6.1编写的api。但所有对此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令牌验证错误的错误是什么?

我错过了什么?

更新 将validationmode设置为端点时,我在身份服务器中遇到异常。

  

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

不幸的是,它没有告诉我api是他预期的...... 如前所述&#34; myApi&#34;被定义为ApiResource但是如何在非核心环境中设置api名称?

2 个答案:

答案 0 :(得分:1)

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

试试这个:

.net4.6.1 应用的Startup.cs中,IdentityServerBearerTokenAuthenticationOptions中有一个属性ValidationMode。将其设置为ValidationMode.Both,然后重试。

答案 1 :(得分:0)

原因是当我使用非核心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。