{"id":47580,"date":"2024-10-07T14:54:13","date_gmt":"2024-10-07T06:54:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/47580\/"},"modified":"2024-10-07T14:54:13","modified_gmt":"2024-10-07T06:54:13","slug":"iis%e6%96%b0%e6%89%8b%e6%8c%87%e5%8d%97%ef%bc%9a%e7%ae%a1%e7%90%86api%e7%af%80%e6%b5%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/47580\/","title":{"rendered":"IIS\u65b0\u624b\u6307\u5357\uff1a\u7ba1\u7406API\u7bc0\u6d41"},"content":{"rendered":"<h1 id=\"iis%e6%96%b0%e6%89%8b%e6%8c%87%e5%8d%97%ef%bc%9a%e7%ae%a1%e7%90%86api%e7%af%80%e6%b5%81-juHmGXCUdN\">IIS\u65b0\u624b\u6307\u5357\uff1a\u7ba1\u7406API\u7bc0\u6d41<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0cAPI\uff08\u61c9\u7528\u7a0b\u5f0f\u4ecb\u9762\uff09\u5df2\u6210\u70ba\u61c9\u7528\u7a0b\u5f0f\u4e4b\u9593\u9032\u884c\u901a\u4fe1\u7684\u95dc\u9375\u7d44\u4ef6\u3002\u7136\u800c\uff0c\u96a8\u8457API\u7684\u4f7f\u7528\u983b\u7387\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406API\u7684\u6d41\u91cf\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u63a2\u8a0e\u5982\u4f55\u5728IIS\uff08Internet Information Services\uff09\u4e2d\u5be6\u73feAPI\u7bc0\u6d41\uff0c\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afapi%e7%af%80%e6%b5%81%ef%bc%9f-juHmGXCUdN\">\u4ec0\u9ebc\u662fAPI\u7bc0\u6d41\uff1f<\/h2>\n<p>API\u7bc0\u6d41\u662f\u6307\u9650\u5236API\u8acb\u6c42\u7684\u983b\u7387\uff0c\u4ee5\u9632\u6b62\u904e\u591a\u7684\u8acb\u6c42\u5c0d\u4f3a\u670d\u5668\u9020\u6210\u58d3\u529b\u3002\u9019\u7a2e\u6280\u8853\u53ef\u4ee5\u5e6b\u52a9\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\uff0c\u9632\u6b62\u56e0\u70ba\u6d41\u91cf\u904e\u5927\u800c\u5c0e\u81f4\u7684\u670d\u52d9\u4e2d\u65b7\u6216\u6027\u80fd\u4e0b\u964d\u3002\u7bc0\u6d41\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u7b56\u7565\u9032\u884c\uff0c\u4f8b\u5982\u57fa\u65bc\u6642\u9593\u7a97\u53e3\u7684\u9650\u5236\u3001\u7528\u6236\u8eab\u4efd\u7684\u9650\u5236\u7b49\u3002<\/p>\n<h2 id=\"iis%e4%b8%ad%e7%9a%84api%e7%af%80%e6%b5%81%e5%af%a6%e7%8f%be-juHmGXCUdN\">IIS\u4e2d\u7684API\u7bc0\u6d41\u5be6\u73fe<\/h2>\n<p>\u5728IIS\u4e2d\u5be6\u73feAPI\u7bc0\u6d41\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u65b9\u5f0f\u9032\u884c\uff0c\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8url%e9%87%8d%e5%af%ab%e6%a8%a1%e7%b5%84-juHmGXCUdN\">1. \u4f7f\u7528URL\u91cd\u5beb\u6a21\u7d44<\/h3>\n<p>IIS\u7684URL\u91cd\u5beb\u6a21\u7d44\u53ef\u4ee5\u7528\u4f86\u8a2d\u7f6e\u8acb\u6c42\u7684\u9650\u5236\u3002\u901a\u904e\u914d\u7f6eweb.config\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c0d\u7279\u5b9a\u7684API\u8def\u5f91\u9032\u884c\u6d41\u91cf\u63a7\u5236\u3002<\/p>\n<pre><code>\n&lt;configuration&gt;\n  &lt;system.webServer&gt;\n    &lt;rewrite&gt;\n      &lt;rules&gt;\n        &lt;rule name=\"Throttle API Requests\"&gt;\n          &lt;match url=\"^api\/.*\"$ \/&gt;\n          &lt;conditions&gt;\n            &lt;add input=\"{REQUEST_METHOD}\" pattern=\"GET\" \/&gt;\n          &lt;\/conditions&gt;\n          &lt;action type=\"CustomResponse\" statusCode=\"429\" statusReason=\"Too Many Requests\" statusDescription=\"You have exceeded your request limit.\" \/&gt;\n        &lt;\/rule&gt;\n      &lt;\/rules&gt;\n    &lt;\/rewrite&gt;\n  &lt;\/system.webServer&gt;\n&lt;\/configuration&gt;\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8asp-net%e4%b8%ad%e9%96%93%e4%bb%b6-juHmGXCUdN\">2. \u4f7f\u7528ASP.NET\u4e2d\u9593\u4ef6<\/h3>\n<p>\u5982\u679c\u60a8\u7684API\u662f\u57fa\u65bcASP.NET\u958b\u767c\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e2d\u9593\u4ef6\u4f86\u5be6\u73fe\u7bc0\u6d41\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\npublic class ThrottleMiddleware\n{\n    private readonly RequestDelegate _next;\n    private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(5); \/\/ \u9650\u5236\u540c\u6642\u8acb\u6c42\u6578\u91cf\n\n    public ThrottleMiddleware(RequestDelegate next)\n    {\n        _next = next;\n    }\n\n    public async Task Invoke(HttpContext context)\n    {\n        if (!await _semaphore.WaitAsync(1000)) \/\/ \u7b49\u5f851\u79d2\n        {\n            context.Response.StatusCode = 429; \/\/ Too Many Requests\n            await context.Response.WriteAsync(\"Too many requests. Please try again later.\");\n            return;\n        }\n\n        try\n        {\n            await _next(context);\n        }\n        finally\n        {\n            _semaphore.Release();\n        }\n    }\n}\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e7%ac%ac%e4%b8%89%e6%96%b9%e5%ba%ab-juHmGXCUdN\">3. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5eab<\/h3>\n<p>\u9084\u6709\u8a31\u591a\u7b2c\u4e09\u65b9\u5eab\u53ef\u4ee5\u5e6b\u52a9\u5be6\u73feAPI\u7bc0\u6d41\uff0c\u4f8b\u5982 <a href=\"https:\/\/github.com\/michaelscodingspot\/ThrottlingMiddleware\" rel=\"nofollow noopener\" target=\"_blank\">ThrottlingMiddleware<\/a>\u3002\u9019\u4e9b\u5eab\u901a\u5e38\u63d0\u4f9b\u4e86\u66f4\u9748\u6d3b\u7684\u914d\u7f6e\u9078\u9805\u548c\u66f4\u5f37\u5927\u7684\u529f\u80fd\u3002<\/p>\n<h2 id=\"api%e7%af%80%e6%b5%81%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-juHmGXCUdN\">API\u7bc0\u6d41\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u8a2d\u5b9a\u5408\u7406\u7684\u9650\u5236\uff1a<\/strong>\u6839\u64da\u7528\u6236\u9700\u6c42\u548c\u4f3a\u670d\u5668\u6027\u80fd\u8a2d\u5b9a\u5408\u7406\u7684\u8acb\u6c42\u9650\u5236\u3002<\/li>\n<li><strong>\u63d0\u4f9b\u6e05\u6670\u7684\u932f\u8aa4\u4fe1\u606f\uff1a<\/strong>\u7576\u7528\u6236\u8d85\u904e\u8acb\u6c42\u9650\u5236\u6642\uff0c\u61c9\u63d0\u4f9b\u6e05\u6670\u7684\u932f\u8aa4\u4fe1\u606f\uff0c\u544a\u77e5\u7528\u6236\u4f55\u6642\u53ef\u4ee5\u518d\u6b21\u5617\u8a66\u3002<\/li>\n<li><strong>\u76e3\u63a7\u548c\u8abf\u6574\uff1a<\/strong>\u5b9a\u671f\u76e3\u63a7API\u7684\u4f7f\u7528\u60c5\u6cc1\uff0c\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u8abf\u6574\u7bc0\u6d41\u7b56\u7565\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-juHmGXCUdN\">\u7e3d\u7d50<\/h2>\n<p>API\u7bc0\u6d41\u662f\u78ba\u4fdd\u7cfb\u7d71\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u7684\u91cd\u8981\u63aa\u65bd\u3002\u5728IIS\u4e2d\uff0c\u53ef\u4ee5\u901a\u904eURL\u91cd\u5beb\u6a21\u7d44\u3001ASP.NET\u4e2d\u9593\u4ef6\u6216\u7b2c\u4e09\u65b9\u5eab\u4f86\u5be6\u73fe\u7bc0\u6d41\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u5b9a\u548c\u76e3\u63a7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406API\u6d41\u91cf\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1IIS\u65b0\u624b\u6307\u5357\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u7ba1\u7406API\u7bc0\u6d41\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u7a69\u5b9a\u6027\uff0c\u78ba\u4fdd\u6d41\u91cf\u63a7\u5236\u8207\u8cc7\u6e90\u6700\u4f73\u5316\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-47580","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/47580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=47580"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/47580\/revisions"}],"predecessor-version":[{"id":47581,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/47580\/revisions\/47581"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=47580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=47580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=47580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}