正确使用MVC5授权过滤器

时间:2018-06-04 18:55:04

标签: asp.net-mvc authentication asp.net-mvc-filters

我正在尝试创建一个只允许本地请求通过的mvc 5过滤器。

过滤器有效,但在某些请求中会产生无限循环。我很冒昧,我可能做错了什么。

public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
  if (!actionContext.Request.IsLocal())
  {
    var task = new Task<HttpResponseMessage>(() =>
    {
      return actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Only local requests are allowed");
    });
    task.Start();
    return task;
  }
  else
  {
    return continuation();
  }
}

有什么想法吗?

0 个答案:

没有答案