我认为这个问题有点琐碎,但我找不到答案:
我将Ocelot用作API网关并使用自己的身份验证中间件,因为我需要根据我们自己的数据库对用户进行身份验证。幸福的道路工作得很好。但是,如果用户无法进行身份验证,我只能返回500个内部服务器错误,但是我不知道如何使ocelot返回401。
有人可以帮助我吗?这将是我的意思的最基本的内容:
var authSuccess = authenticate();
if (authSuccess)
{
await next.Invoke();
} else
{
// cancel and Return 401
}
答案 0 :(得分:0)
好的,所以我进一步研究了回购协议,发现了一些对我有帮助的东西:
AuthenticationMiddleware = async (context, next) =>
{
var authSuccess = authenticate();
if (authSuccess)
{
await next.Invoke();
} else
{
var error = new UnauthenticatedError("Some Message");
context.Errors.Add(error);
}
}
答案 1 :(得分:0)
您可以通过
await ctx.HttpContext.Response.WriteAsync("{'error':'Your message'}");
ctx.Errors.Add(new UnauthenticatedError("Your message"));