Identityserver4 使用引用令牌或JWT进行api验证

Identityserver4 使用引用令牌或JWT进行api验证,identityserver4,Identityserver4,我正在尝试从Identity server 4获取一个api来验证令牌(jwt和引用)。令牌的第一部分如下所示: { alg: "RS256", kid: "09E9884444C4E3221983C13333333333333334A", typ: "JWT", x5t: "CemILYTE3432GGGGGGGGGGGG" }. { nbf: 1522084412, exp: 1522088012, iss: "https://authserv:8097", aud: [

我正在尝试从Identity server 4获取一个api来验证令牌(jwt和引用)。令牌的第一部分如下所示:

{
 alg: "RS256",
 kid: "09E9884444C4E3221983C13333333333333334A",
 typ: "JWT",
 x5t: "CemILYTE3432GGGGGGGGGGGG"
}.
{
 nbf: 1522084412,
 exp: 1522088012,
 iss: "https://authserv:8097",
 aud: [
  "https://authserv:8097/resources",
  "customAPI"
 ],
 client_id: "program",
 sub: "user1",
 auth_time: 1522084412,
 idp: "local",
 scope: [
  "openid",
  "scope1"
 ],
 amr: [
  "pwd"
 ]
}.
   services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "https://authserv:8097";
                options.SupportedTokens = SupportedTokens.Both;
                options.RequireHttpsMetadata = false;
           //     options.ApiName = "CustomAPI";
           //     options.ApiSecret = "secret";

            });

    services.AddMvc(options =>
    {
        var policy = ScopePolicy.Create("scope1");
        options.Filters.Add(new AuthorizeFilter(policy));
    });
startup.cs中的设置如下所示:

{
 alg: "RS256",
 kid: "09E9884444C4E3221983C13333333333333334A",
 typ: "JWT",
 x5t: "CemILYTE3432GGGGGGGGGGGG"
}.
{
 nbf: 1522084412,
 exp: 1522088012,
 iss: "https://authserv:8097",
 aud: [
  "https://authserv:8097/resources",
  "customAPI"
 ],
 client_id: "program",
 sub: "user1",
 auth_time: 1522084412,
 idp: "local",
 scope: [
  "openid",
  "scope1"
 ],
 amr: [
  "pwd"
 ]
}.
   services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "https://authserv:8097";
                options.SupportedTokens = SupportedTokens.Both;
                options.RequireHttpsMetadata = false;
           //     options.ApiName = "CustomAPI";
           //     options.ApiSecret = "secret";

            });

    services.AddMvc(options =>
    {
        var policy = ScopePolicy.Create("scope1");
        options.Filters.Add(new AuthorizeFilter(policy));
    });
在这种形式下,Jason令牌起作用。如果我取消对apiName和apiSecret的注释,JWT将不再工作,并给出“受众无效”错误。使用apiName和apisecret取消注释,引用令牌将正确验证。我是否在配置中遗漏了一些东西,使JWT在apiName和APISCREET未注释的情况下有效

我正在尝试获得一个环境设置,在这个环境中,我可以让identity server使用JWT或引用令牌,具体取决于参数的设置方式以及api将使用哪一种。
提前感谢您的帮助。

您的问题是打字错误


在您的JWT中,我看到受众(aud声明)是
customAPI
(小写字母“c”),而在您的客户端配置中,API名称是
customAPI
(大写字母“c”)。使它们保持一致。

你的问题是打字错误


在您的JWT中,我看到受众(aud声明)是
customAPI
(小写字母“c”),而在您的客户端配置中,API名称是
customAPI
(大写字母“c”)。使它们相同。

如果只注释掉选项,会发生什么情况?还有-你使用的是哪种补助金类型?是客户端凭据吗?如果我只注释掉api机密,那么JWT也会出现同样的错误。授予类型是隐式的。谢谢您是否尝试下面的mi答案?如果您只注释掉
options.ApiSecret=“secret”?还有-你使用的是哪种补助金类型?是客户端凭据吗?如果我只注释掉api机密,那么JWT也会出现同样的错误。授予类型是隐式的。谢谢你能试试下面的我的答案吗?我花了两天时间处理一个无法理解的身份验证错误。你重拍了我的一天,我的观众是container,ContainerI花了两天时间处理一个无法理解的身份验证错误。你重塑了我的一天,我的观众是一个又一个容器