Ocelot与Jwt Auth

时间:2019-07-18 15:11:20

标签: c# .net-core ocelot

我正在ocelot(asp.net核心中的API网关)中使用jwt auth设置身份验证,但未成功。我在邮递员中执行我的代码,但是输出与预期不符。

这是Startup.cs中的ConfigureServices 我从appsettings.json获取ApiKey,并添加了Jwt的参数,例如Issuer,Audience和IssuerSigningKey。

public void ConfigureServices(IServiceCollection services)
        {
            //security key
            string securityKey = Configuration.GetValue<string>("ApiKey:DefaultKey");

            //symmetric security key
            var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));

            services.AddDbContext<JwtEPDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateIssuerSigningKey = true,

                        ValidIssuer = "smesk.in",
                        ValidAudience = "readers",
                        IssuerSigningKey = symmetricSecurityKey
                    };
                });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddOcelot(Configuration);
        }

和这个ocelot.json


{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 3000
        }
      ],
      "UpstreamPathTemplate": "/api/this-app",
      "AuthenticationOptions": {
        "SecurityKey": "0987654123456qwertysdfgvbniastrtyhajsbc8726ghuaiysdyt1276"
      }

    },

    {
      "DownstreamPathTemplate": "/api/svc_execScript2",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "10.12.4.155",
          "Port": 990

        }
      ],
      "UpstreamPathTemplate": "/api/test-ep"

    },

    {
      "DownstreamPathTemplate": "/api/svc_execScript2",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "10.12.4.155",
          "Port": 990

        }
      ],
      "UpstreamPathTemplate": "/api/test-login"

    }

  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:44319"

  }


}

我希望输出是401响应,但实际输出是200

0 个答案:

没有答案