Ahmad Elsalahat
Ahmad Elsalahat

Reputation: 25

Prevent download files from URL

I've Blazor Application (Blazor Server) with side menu. When you click on one of these menus, you will open PDF file based on specific privilege (when clicks on href).

My question :- what if someone changes the URL manually and replace it by the file URL, how I can get this URL or prevent unauthorized user from downloading this file ??

Upvotes: 0

Views: 647

Answers (2)

Sarang Kulkarni
Sarang Kulkarni

Reputation: 367

public FileResult DownloadFile()
{
    // logic to allow/disallow users from downloading
    byte[] fileBytes = System.IO.File.ReadAllBytes(@"pathtofile"); // or any other source
    string fileName = "nameWithExtension";
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

While link to this method in controller can be presented to end users

Upvotes: 0

Nicola Biada
Nicola Biada

Reputation: 2800

It's better to create a controller to download file, so you can control the download before it starts.

Something like:

<a href="/files/download?name=myfile.pdf">My File</a>

In this case the FilesController will have a Download method and inside this method you can check the authorization process.

Upvotes: 2

Related Questions