1、Nugetは、WebAPIののプロジェクトWebApiThrottleへの参照を追加します
一般的に次のように登録する方法WebApiConfigの、追加で2、登録、:
config.Filters.Add(新しいCustomThrottlingFilter() { ポリシー = 新しいThrottlePolicy() { // IP設定エリア IpThrottling = trueに、 ClientThrottling = 真、 // エンドポイントの制限ポリシーの設定はEnableThrottlingプロパティから取得します。 = EndpointThrottling trueに } });
ThrottlingFilterのどのCustomThrottlingFilter自身の書き換え、あなたも直接、デフォルトの設定を使用することができます。マイカスタムCustomThrottlingFilterは次の通り:
パブリック クラスCustomThrottlingFilter:ThrottlingFilter { /// <要約> ///はindentityを設定します。 /// </要約> /// ます。<param name = "要求"> 要求。</ param>の /// <リターン> RequestIdentity。</リターン> 保護 オーバーライドRequestIdentity SetIdentity(HttpRequestMessageリクエスト) { VARセッションID = 文字列.Empty。 試します { VAR requestCookie = 。request.Headers.GetCookies()FirstOrDefault(); もし(requestCookie!= nullの) { foreachの(VARの項目に requestCookie.Cookies.Where(項目=> item.Name == " SESSION_ID " )) { セッションID = item.Value。 休憩; } } } キャッチ(例外) { セッションID = 文字列.Empty。 } 返す 新しいRequestIdentityを() { ClientKey = 文字列 .IsNullOrWhiteSpace(セッションID)?セッションID:「アノン」、 クライアントIP = ベース.GetClientIp(リクエスト).ToString()、 エンドポイント = request.RequestUri.AbsolutePath.ToLowerInvariant() }。 } }
3、又はマーキングヘッドを制御するために必要なインターフェースのコントローラプラス
[EnableThrottling(perminute = 12)] // 制御アクセス頻度、毎分最大12倍
あなたはアクセス頻度を制御する必要がプラスなしあり
[DisableThrotting]
ソース https://www.cnblogs.com/SzeCheng/p/5407316.html