如何在Web Api中限制请求?

抢夺

我正在尝试通过以下方式实施请求限制:

在ASP.NET MVC中实现请求限制的最佳方法?

我已将该代码放入解决方案中,并使用属性装饰了API控制器端点:

[Route("api/dothis/{id}")]
[AcceptVerbs("POST")]
[Throttle(Name = "TestThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
[Authorize]
public HttpResponseMessage DoThis(int id) {...}

这样可以编译,但是属性的代码未命中,并且节流也不起作用。我没有任何错误。我想念什么?

达林·迪米特罗夫(Darin Dimitrov)

您似乎对ASP.NET MVC控制器的动作过滤器和ASP.NET Web API控制器的动作过滤器感到困惑。这是2个完全不同的类:

您似乎看到的是Web API控制器操作(在衍生自的控制器内部声明的操作ApiController)。因此,如果您要对其应用自定义过滤器,则它们必须从派生System.Web.Http.Filters.ActionFilterAttribute

因此,让我们继续为Web API修改代码:

public class ThrottleAttribute : ActionFilterAttribute
{
    /// <summary>
    /// A unique name for this Throttle.
    /// </summary>
    /// <remarks>
    /// We'll be inserting a Cache record based on this name and client IP, e.g. "Name-192.168.0.1"
    /// </remarks>
    public string Name { get; set; }

    /// <summary>
    /// The number of seconds clients must wait before executing this decorated route again.
    /// </summary>
    public int Seconds { get; set; }

    /// <summary>
    /// A text message that will be sent to the client upon throttling.  You can include the token {n} to
    /// show this.Seconds in the message, e.g. "Wait {n} seconds before trying again".
    /// </summary>
    public string Message { get; set; }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var key = string.Concat(Name, "-", GetClientIp(actionContext.Request));
        var allowExecute = false;

        if (HttpRuntime.Cache[key] == null)
        {
            HttpRuntime.Cache.Add(key,
                true, // is this the smallest data we can have?
                null, // no dependencies
                DateTime.Now.AddSeconds(Seconds), // absolute expiration
                Cache.NoSlidingExpiration,
                CacheItemPriority.Low,
                null); // no callback

            allowExecute = true;
        }

        if (!allowExecute)
        {
            if (string.IsNullOrEmpty(Message))
            {
                Message = "You may only perform this action every {n} seconds.";
            }

            actionContext.Response = actionContext.Request.CreateResponse(
                HttpStatusCode.Conflict, 
                Message.Replace("{n}", Seconds.ToString())
            );
        }
    }
}

其中GetClientIp方法来自this post

现在,您可以在Web API控制器操作上使用此属性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Web Api中限制请求?

来自分类Dev

如何在 Web API 中限制 POST

来自分类Dev

如何使用RxJ限制Angular中的并发API请求

来自分类Dev

如何限制API请求的堆栈?

来自分类Dev

如何限制API请求的堆栈?

来自分类Dev

如何在angularjs中限制$ http请求?

来自分类Dev

如何在我的应用程序Angular 9中限制API请求

来自分类Dev

如何在Web API中维护状态或请求队列

来自分类Dev

并发请求限制为.Net MVC / Web Api中的10个

来自分类Dev

如何在Rails中记录API请求?

来自分类Dev

如何在Web API中更新集合?

来自分类Dev

WEB API 是如何在JS 中实现的?

来自分类Dev

如何在Web API授权属性中获取请求cookie?

来自分类Dev

如何在Web Api Post请求Json中检测重复密钥

来自分类Dev

如何在Web API中捕获请求和响应数据调用?

来自分类Dev

如何在.NET Web API 2中检索所有请求的参数?

来自分类Dev

如何在ASP.NET Web API 2中具有可选的请求模型属性?

来自分类Dev

ASP.NET MVC Web API请求限制

来自分类Dev

限制由ASP.NET Web API服务的同时请求

来自分类Dev

ASP.NET MVC Web API请求限制

来自分类Dev

在Azure中请求Web API请求超时

来自分类Dev

速率限制如何在Search API中的Twitter中工作

来自分类Dev

速率限制如何在Search API中的Twitter中工作

来自分类Dev

如何在Azure Web角色中防止重复的HTTP请求

来自分类Dev

如何在Azure Web角色中防止重复的HTTP请求

来自分类Dev

如何增强Box.com API请求限制

来自分类Dev

如何增强Box.com API请求限制

来自分类Dev

Symfony 上的 API 请求限制,如何最好地实现?

来自分类Dev

如何在Twitter的流式API中检查速率限制