【问题标题】:Ocelot Gateway authentication with OKTA使用 OKTA 进行 Ocelot 网关身份验证
【发布时间】:2021-10-18 16:35:31
【问题描述】:

我是身份服务器的新手。我实际上是在尝试对 Okta 提供者从 Ocelot 网关生成的 JWT 令牌进行身份验证,并在身份验证成功后允许访问底层 API。

我实际上可以使用 Postman 成功生成 ID 和访问令牌。但是我用token传给Ocelot网关总是显示401 unauthorized错误。

这是来自 Ocelot 网关的代码。

代码

public void ConfigureServices(IServiceCollection services)
    {
        services
 .AddAuthentication()
 .AddJwtBearer("Bearer", options =>
 {
     options.Audience = "api://default"; // Okta Authorization server Audience
     options.Authority = "https://dev-12345678.okta.com/"; // Okta Authorization Issuer URI URL e.g. https://{subdomain}.okta.com/oauth2/{authidentifier}
     options.RequireHttpsMetadata = false;
 });
        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("scp");
        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Add("scp", "scope");
        services.AddOcelot(Configuration);
        IdentityModelEventSource.ShowPII = true;
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseAuthentication().UseOcelot().Wait();
        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

【问题讨论】:

    标签: .net-core identityserver4 okta ocelot


    【解决方案1】:

    我会查看 Configure 方法中项目的顺序。我会将 UseRouting 作为首要项目之一,例如:

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
    
        app.UseRouting();
        app.UseHttpsRedirection();
    
        app.UseAuthentication();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    
        app.UseOcelot();
    

    通过这样做search查看 Github 中的现有示例

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2020-06-22
      • 2018-07-16
      • 2017-10-31
      • 2022-09-29
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多