Ocelot总是未经授权的Authentication Providerkey

发布于 2025-02-07 18:59:29 字数 2711 浏览 0 评论 0原文

我的问题Ocelot仅在添加AuthenticationProviderKey后才给出未经授权的错误。 Ocelot文件:

{
"Routes": [
  {
    "DownstreamPathTemplate": "/api/blog",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7001
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/blog",
    "UpstreamHttpMethod": [ "Get" ],
    "AuthenticationOptions": {
      "AuthenticationProviderKey": "TestKey",
      "AllowedScopes": []
    }
  },
  {
    "DownstreamPathTemplate": "/api/notfication",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7011
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/notfication",
    "UpstreamHttpMethod": [ "Get", "Post" ],
  },
  {
    "DownstreamPathTemplate": "/api/user",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7021
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/user",
    "UpstreamHttpMethod": [ "Get", "Post" ]
  },
  {
    "DownstreamPathTemplate": "/api/auth",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7031
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/auth",
    "UpstreamHttpMethod": [ "Post" ],
  }
],

“ GlobalConfiguration”:{ “ baseurl”:“ https:// localhost:7000” } }

当我删除位于Ocelot中的AuthenticationProviderKey时, Apigateway program.csİt

    string authenticationProviderKey = "TestKey";
builder.Services.AddAuthentication(
    //option => {
    //    option.DefaultAuthenticateScheme = authenticationProviderKey;
    //    option.DefaultScheme = authenticationProviderKey;
    //    option.DefaultChallengeScheme = authenticationProviderKey;
    //}
    )
    .AddJwtBearer(authenticationProviderKey, options =>
     {
         options.RequireHttpsMetadata = false;
         options.TokenValidationParameters = new TokenValidationParameters
         {
             ValidateIssuerSigningKey = true,
             IssuerSigningKey = signInKey,
             ValidateIssuer = true,
             ValidIssuer = builder.Configuration["JWT:Issuer"],
             ValidateAudience = true,
             ValidAudience = builder.Configuration["JWT:Audience"],
             ValidateLifetime = true,
             ClockSkew = TimeSpan.Zero,
             RequireExpirationTime = true
         };
     });

builder.Services.AddOcelot();
builder.Services.AddControllers();
 
.
.
.
app.UseAuthentication();
app.UseAuthorization();
await app.UseOcelot();

没有给出未经授权的错误。让我注意,其他服务没有问题。因为他不申请。 Apigateway API存在问题。 怎么了?

My problem ocelot gives only unauthorized erroru after adding AuthenticationProviderKey.
Ocelot file:

{
"Routes": [
  {
    "DownstreamPathTemplate": "/api/blog",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7001
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/blog",
    "UpstreamHttpMethod": [ "Get" ],
    "AuthenticationOptions": {
      "AuthenticationProviderKey": "TestKey",
      "AllowedScopes": []
    }
  },
  {
    "DownstreamPathTemplate": "/api/notfication",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7011
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/notfication",
    "UpstreamHttpMethod": [ "Get", "Post" ],
  },
  {
    "DownstreamPathTemplate": "/api/user",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7021
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/user",
    "UpstreamHttpMethod": [ "Get", "Post" ]
  },
  {
    "DownstreamPathTemplate": "/api/auth",
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 7031
      }
    ],
    "UpstreamPathTemplate": "/api/gateway/auth",
    "UpstreamHttpMethod": [ "Post" ],
  }
],

"GlobalConfiguration": {
"BaseUrl": "https://localhost:7000"
}
}

APIGateway Program.cs

    string authenticationProviderKey = "TestKey";
builder.Services.AddAuthentication(
    //option => {
    //    option.DefaultAuthenticateScheme = authenticationProviderKey;
    //    option.DefaultScheme = authenticationProviderKey;
    //    option.DefaultChallengeScheme = authenticationProviderKey;
    //}
    )
    .AddJwtBearer(authenticationProviderKey, options =>
     {
         options.RequireHttpsMetadata = false;
         options.TokenValidationParameters = new TokenValidationParameters
         {
             ValidateIssuerSigningKey = true,
             IssuerSigningKey = signInKey,
             ValidateIssuer = true,
             ValidIssuer = builder.Configuration["JWT:Issuer"],
             ValidateAudience = true,
             ValidAudience = builder.Configuration["JWT:Audience"],
             ValidateLifetime = true,
             ClockSkew = TimeSpan.Zero,
             RequireExpirationTime = true
         };
     });

builder.Services.AddOcelot();
builder.Services.AddControllers();
 
.
.
.
app.UseAuthentication();
app.UseAuthorization();
await app.UseOcelot();

İt is not give unauthorized error when I delete AuthenticationProviderKey located in the ocelot. Let me note that there are no problems with other services. Because he does not apply there. There is a problem with ApiGateway API.
What's wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文