Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Ocelot';什么是预验证中间件?_C#_Api Gateway_Asp.net Core Middleware_Ocelot - Fatal编程技术网

C# 如何在Ocelot';什么是预验证中间件?

C# 如何在Ocelot';什么是预验证中间件?,c#,api-gateway,asp.net-core-middleware,ocelot,C#,Api Gateway,Asp.net Core Middleware,Ocelot,在ocelot身份验证之前,我必须对请求的jwt进行一些检查,因此我将在 var config = new OcelotPipelineConfiguration { PreAuthenticationMiddleware = async (ctx, next) => { var jwt = ctx.HttpContext.Request.Headers["Authorization"]; if (jwtIsOk(jwt)) next.In

在ocelot身份验证之前,我必须对请求的jwt进行一些检查,因此我将在

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继承创建自己的一个。

它们是两个不同的东西。Next调用Next中间件。如果要添加到响应中,应使用
ctx.HttpContext.response.WriteAsync(…)
对其进行写入,并设置状态代码。执行此操作后不调用next将导致管道终止。