/// <summary> /// Load ClientThrollingPolicy from json data to cache memory. /// </summary> /// <param name="FolderName">Container folder of throttle data json file e.g. Resources</param> /// <param name="FileName">File name of throttle data json file e.g. ThrottleData.json</param> public static void CacheClientThrollingPolicy(string FolderName, string FileName) { try { var oList = new List <ClientRateLimiting>(); var jsonfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FolderName, FileName); var jsontext = File.ReadAllText(jsonfile, Encoding.Default); List <Client> clients = JsonConvert.DeserializeObject <List <Client> >(jsontext); MemoryCacherHelper.Add("ClientThrollingPolicy", clients, DateTimeOffset.MaxValue); } catch (Exception ex) { Trace.TraceError(ex.Message); } }
public override void OnActionExecuting(HttpActionContext actionContext) { string endPointName = actionContext.Request.RequestUri.AbsolutePath; #region STEP 1: Get user details like email, IP address, customerid, etc. & fill dictionary requestIdentity = GetIdentity(actionContext); Dictionary <string, string> dictIdentity = new Dictionary <string, string>(); dictIdentity.Add(KeyType.clientKey.ToString(), requestIdentity.clientKey); dictIdentity.Add(KeyType.ip.ToString(), requestIdentity.ipAddress); #endregion #region STEP 2: Get ThrottleData.json file data which is saved in http memory cache. List <Client> clients = (List <Client>)MemoryCacherHelper.GetValue(allClientThrollingPolicyCacheData); Client client = clients.Where(x => x.clientKey == requestIdentity.clientKey).FirstOrDefault(); ThrottlePolicy throttlePolicy = client != null ? client.throttlePolicy : clients[0].throttlePolicy; #endregion #region STEP 3: Filtered whiteList users, throttling not applied to these users. if (throttlePolicy.whiteListClients.Contains(requestIdentity.clientKey) || throttlePolicy.whiteListClients.Contains(requestIdentity.ipAddress)) { return; } #endregion #region Step 4: Apply policy -> unique -> method name + identity key like emil, ip etc. ClientRateLimiting clientRateLimiting = throttlePolicy.clientRateLimiting.Where(p => p.key == _ThrottlePolicyKey).FirstOrDefault(); if (clientRateLimiting != null && !string.IsNullOrEmpty(dictIdentity[clientRateLimiting.keyType.ToString()])) { ApplyAndCheckPolicy(actionContext, clientRateLimiting.policy, dictIdentity[clientRateLimiting.keyType.ToString()] + endPointName); Trace.TraceInformation("******************Identity=>" + clientRateLimiting.keyType.ToString() + " => " + dictIdentity[clientRateLimiting.keyType.ToString()] + "/Method Call=>" + actionContext.Request.RequestUri.AbsoluteUri); } #endregion }