gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:43:45 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com