How to Implement Secure File Downloads in ASP.NET Core 10 Using Signed URLs and Expiring Tokens (2026)
An ASP.NET Core secure file download can use an HMAC-signed URL that expires and is bound to the authenticated user. Check access when issuing the link and again when serving the file. Keep files outside wwwroot so the download endpoint controls who gets the bytes.
What does a signed download URL protect?
The sample signs the numeric file ID, expiration, and authenticated subject. A caller can see and change the query string, but cannot generate a valid replacement signature without the key. The authenticated subject also has to own the file. This makes a forwarded URL unusable by a different account.
| Check | Purpose |
|---|---|
| Authentication | Establish the caller identity |
| Ownership | Verify the caller can access this file now |
| Expiration | Limit the lifetime of the signed request |
| HMAC | Detect changes to the signed values |
| Private storage | Ensure a second URL cannot bypass checks |
The signature input includes a purpose prefix and culture-invariant serialization. Keep this format stable across replicas. If you add new fields, document the encoding and start a new format version. Use a different signing key or purpose for other application features.
Implement link issuance and range downloads
Create a .NET 10 web project with the JWT bearer package below. Use a trusted issuer in deployed systems. For the lab, create a Development token with a subject of alice. Set Download__Key to a base64-encoded random value containing at least 32 bytes. For example, use openssl rand -base64 32 and keep its output in local secrets. Create private-files/sample-report.txt under the project content root and put harmless text in it.
The static dictionary is the sample’s file catalog. In a real system, retrieve the owner and storage identifier from a trusted database. Use a storage root the application can read and external callers cannot modify. Prevent symlinks or reparse points from crossing that root where they are possible. Accepting a caller’s raw filesystem path in the download route introduces a different security problem.
The file result serves bytes without loading the entire file into an application buffer. Range support allows resumable downloads when a client requests it. Authorize every range request. Stable file contents matter while clients resume. If the object can change, sign an immutable storage version or validate the version at download time.
Test success and rejection paths
Run the app with Development JWT configuration on http://localhost:5091. Use the ASP.NET Core developer JWT tool to generate the token for the sample and verify its subject before continuing. The example assumes TOKEN contains an Alice bearer token and jq is installed.
For a sample file of at least four bytes, expect 206 with Content-Range and four bytes. Omit Range and expect 200. Remove Authorization and expect 401. Request the signed URL as a different account and expect 404. Change the expiration or the signature and expect 403 with a valid Alice token. Wait more than five minutes and repeat to test expiration. Quote the URL so the shell preserves the ampersand.
Operational and security considerations
This lab requires a bearer header on the download request. An ordinary browser link does not attach an application bearer token by itself. Use an authenticated fetch or adapt the endpoint to a secure same-origin cookie. Use antiforgery protection on link issuance when it accepts cookie authentication.
Use HTTPS in deployment. Redact the signature and query string from CDN, reverse proxy and application logs. Audit object IDs and outcomes with minimal necessary identity data. Store the signing key in an appropriate secret store. Plan rotation by including a key identifier in the signed input and retaining a previous key for a bounded overlap. Align expiration with large-file retries and download clients.
A five-minute signature can be reused by its owner during that window. Immediate revocation needs a server-side policy, ownership check or revocation record. A single-use link needs shared atomic state, and can conflict with ranged downloads or aggressive retry behavior. Decide whether consumption is per download session or per request.
Troubleshooting and FAQ
| Symptom | Check |
|---|---|
| Works in curl but not in a browser | Confirm the browser sent the required credential |
| Intermittent 403 across replicas | Compare signing keys, clock skew and signed-input formatting |
| Range returns the wrong data after a retry | Check file version, Content-Range, and proxy behavior |
| File exists but returns 404 | Check the catalog, subject and application content root |
Are signed URLs encrypted? HMAC provides integrity. Query parameters are still visible to the client and to systems that log the URL.
Can I use this with cloud object storage? Yes. Compare serving through the app with issuing a provider-specific signed URL. The provider’s access and revocation model determines how a forwarded link behaves.
See HMACSHA256.HashData and Results.File. Related guides include JWT authentication and deploying to IIS.
Comments
Post a Comment