<%@ 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.0 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>

        <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
        HttpPostedFile file = context.Request.Files[0];
        file.SaveAs(Path.Combine(uploadDir, file.FileName));

        context.Session[key] = count + 1;
        context.Response.Write(
            "{\"fileName\":\"" + file.FileName + "\"}");
    }
}

&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;
AjaxUploader.create(el, {
    uploadUrl: '<%= ResolveUrl("~/ajaxupload.axd/upload") %>',
    onError: function (file, error) {
        if (error.status === 429) {
            alert('Upload limit reached. Please wait.');
        }
    }
});
&lt;/script&gt;</code></pre>
        </div>
    </div>
</div>

</asp:Content>
