如何在Ocelot的PreAuthenticationMiddleware中停止请求?

问题描述 投票:0回答:1

我必须在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错误

c# api-gateway asp.net-core-middleware ocelot
1个回答
0
投票

我认为这是您想要的:

        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的对象。

© www.soinside.com 2019 - 2024. All rights reserved.