From b72790783b869dbe312f7e822d81ecf31c8ae11d Mon Sep 17 00:00:00 2001 From: "Gui.H" <740360381@qq.com> Date: Sat, 24 Apr 2021 21:04:26 +0800 Subject: [PATCH] Update ThrottlingHandler.cs Make Authorization-Token Configurable --- WebApiThrottle/ThrottlingHandler.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/WebApiThrottle/ThrottlingHandler.cs b/WebApiThrottle/ThrottlingHandler.cs index 93e5bb1..931f333 100644 --- a/WebApiThrottle/ThrottlingHandler.cs +++ b/WebApiThrottle/ThrottlingHandler.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -129,6 +129,22 @@ public ThrottlePolicy Policy /// public HttpStatusCode QuotaExceededResponseCode { get; set; } + private string tokenKey = "Authorization-Token"; + + public string AuthorizationToken + { + get { return tokenKey; } + set + { + if (string.IsNullOrEmpty(tokenKey)) + { + throw new ArgumentNullException(nameof(AuthorizationToken)); + } + + tokenKey = value; + } + } + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // get policy from repo @@ -227,8 +243,8 @@ protected virtual RequestIdentity SetIdentity(HttpRequestMessage request) var entry = new RequestIdentity(); entry.ClientIp = core.GetClientIp(request).ToString(); entry.Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant(); - entry.ClientKey = request.Headers.Contains("Authorization-Token") - ? request.Headers.GetValues("Authorization-Token").First() + entry.ClientKey = request.Headers.Contains(AuthorizationToken) + ? request.Headers.GetValues(AuthorizationToken).First() : "anon"; return entry;