服务堆栈终止筛选器中的HTTP请求

本文关键字:HTTP 请求 堆栈 终止 筛选 服务 | 更新日期: 2023-09-27 17:58:01

有没有一种方法可以在过滤器中杀死HTTPRequest。I以下代码:

this.RequestFilters.Add((req, res, requestDto) =>
{                
    var access_token = req.Headers.GetValues("token");
    if(access_token == null || String.IsNullOrEmpty(access_token[0]))
    {
        //Kill Request here, ensuring it does not hit the service and returns an unauthorized       error code
    }

服务堆栈终止筛选器中的HTTP请求

使用EndRequest()结束请求并缩短请求管道,例如:

this.RequestFilters.Add((req, res, requestDto) =>
{                
    var access_token = req.Headers.GetValues("token");
    if (access_token == null || String.IsNullOrEmpty(access_token[0]))
        res.EndRequest()
}

@rosdi是正确的,答案是执行以下操作:

this.RequestFilters.Add((req, res, requestDto) =>
{                
    var access_token = req.Headers.GetValues("token");
    if(access_token == null || String.IsNullOrEmpty(access_token[0]))
    {
        throw new UnauthorizedAccessException();
    }