<%@ Page Title="Rate Limiting" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %> 
<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">Rate Limiting - AjaxUploader v5.2 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">Rate Limiting</asp:Content> 
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 
<div class="demo-card"> <div class="demo-card-header">Rate Limiting</div> <div class="demo-card-body"> <p class="demo-description"> Apply rate limiting to upload endpoints to prevent abuse. Limit the number of
 uploads per user per time window using session-based tracking in the upload handler.
 </p> <div class="demo-expect"><strong>What you should see:</strong> rapid repeat uploads are throttled server-side - hammer the button and later requests are rejected with a rate-limit response.</div> <au:AjaxFileUpload ID="Uploader1" runat="server"
 AllowMultiple="true"
 AutoUpload="true"
 ShowProgress="true"
 ButtonText="Rate-Limited Upload" /> <div class="code-panel"> <pre><code>&lt;%-- Upload handler with session-based rate limiting --%&gt;
public class UploadHandler : IHttpHandler, IRequiresSessionState
{
 private const int MaxUploadsPerMinute = 10;

 public void ProcessRequest(HttpContext context)
 {
 // Track uploads per session
 var key = "UploadCount";
 var timestampKey = "UploadWindowStart";

 DateTime windowStart = context.Session[timestampKey]
 as DateTime? ?? DateTime.MinValue;
 int count = context.Session[key] as int? ?? 0;

 // Reset window if expired
 if ((DateTime.UtcNow - windowStart).TotalMinutes &gt;= 1)
 {
 count = 0;
 windowStart = DateTime.UtcNow;
 context.Session[timestampKey] = windowStart;
 }

 if (count &gt;= MaxUploadsPerMinute)
 {
 context.Response.StatusCode = 429;
 context.Response.Write(
 "Too many uploads. Please try again later.");
 return;
 }

 // Process upload
 if (context.Request.Files.Count == 0)
 {
 context.Response.StatusCode = 400;
 context.Response.Write("No file was posted.");
 return;
 }

 HttpPostedFile file = context.Request.Files[0];
 string safeName = Path.GetFileName(file.FileName ?? string.Empty);
 if (safeName.Length == 0)
 {
 context.Response.StatusCode = 400;
 context.Response.Write("Invalid file name.");
 return;
 }
 file.SaveAs(Path.Combine(uploadDir, safeName));

 context.Session[key] = count + 1;
 context.Response.ContentType = "application/json";
 context.Response.Write(
 "{\"fileName\":\"" +
 HttpUtility.JavaScriptStringEncode(safeName) + "\"}");
 }
}

&lt;%-- web.config: configure rate limit settings --%&gt;
&lt;appSettings&gt;
 &lt;add key="UploadRateLimit" value="10" /&gt;
 &lt;add key="UploadRateWindowSeconds" value="60" /&gt;
&lt;/appSettings&gt;

&lt;%-- Client-side: handle 429 status --%&gt;
&lt;script&gt;
var statusEl = document.getElementById('uploadStatus');
AjaxUploader.create(el, {
 uploadUrl: '<%= ResolveUrl("~/ajaxupload.axd/upload") %>',
 onError: function (file, error) {
 if (error.status === 429) {
 statusEl.textContent = 'Upload limit reached. Please wait.';
 }
 }
});
&lt;/script&gt;</code></pre> </div> </div>
</div> 
</asp:Content>
