c# - 如何停止 Ocelot 的 PreAuthenticationMiddleware 中的请求?

标签 c# api-gateway asp.net-core-middleware ocelot

我必须在 ocelot 身份验证之前对请求的 jwt 进行一些检查,所以我在 PreAuthenticationMiddleware 中进行检查像这样:

var config = new OcelotPipelineConfiguration
{
    PreAuthenticationMiddleware = async (ctx, next) =>
    {
        var jwt = ctx.HttpContext.Request.Headers["Authorization"];

        if (jwtIsOk(jwt)) next.Invoke();
        // else ...
    }
};

app.UseOcelot(config).Wait();

是否可以返回自定义响应,而不是不调用 next

我想返回一个 401 错误

最佳答案

我想这就是您要找的:

        var configuration = new OcelotPipelineConfiguration
        {
            PreAuthenticationMiddleware = async (ctx, next) =>
            {
                if (xxxxxx) 
                {
                    ctx.Errors.Add(new UnauthenticatedError("Your message"));

                    return;
                }

                await next.Invoke();
            }
        };

在这种情况下,我使用的是 UnauthenticatedError(一个 Ocelot 的类),它将以 401 结尾。还有更多类似的错误。您还可以创建自己的继承自 Ocelot.Errors.Error。

关于c# - 如何停止 Ocelot 的 PreAuthenticationMiddleware 中的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56772376/

相关文章:

c# - 在 ASP.NET Core 3.1 中使用带有 HttpContext.Response 的新 Json 序列化器

c# - 静态异步类的模拟单元测试和依赖注入(inject)

c# - UWP Raspberry Pi 网络服务器问题

spring - 在没有数据库的 JHipster 微服务架构中创建网关

c# - Angular:无法通过 HttpResponse 返回自定义错误消息

c# - 如何在 Asp.Net 中间件中抛出错误

c# - 为什么枢轴项中的 Frame 属性为空?

c# - .NET Core 的可选构造函数注入(inject)参数

go - 如何配置 krakend.json 转发参数化 url?

google-cloud-platform - 使用 Google Cloud Functions 实现微服务的 API 网关