I need help on how to protect/secure my Api upload route when an upload is performed.
In a razor page I have a RadzenUpload
control like this:
<RadzenUpload Accept="image/*" Progress=@OnProgress class="w-100" Url=@($"/api/upload/uploadfirmlogo")/>
So the upload will be posted at /api/upload/uploadfirmlogo
and this is my controller:
[ApiController]
[Microsoft.AspNetCore.Mvc.Route("api/[controller]")]
//[Authorize]
public class UploadController : Controller
{
private readonly IWebHostEnvironment environment;
public UploadController(IWebHostEnvironment environment)
{
this.environment = environment;
}
[HttpPost("UploadFirmLogo")]
public async Task<IActionResult> UploadFirmLogoAsync(IFormFile[] files)
{
try
{
if (HttpContext.Request.Form.Files.Any())
{
var file = HttpContext.Request.Form.Files[0];
string path = Path.Combine(environment.WebRootPath, "images", "logos", file.FileName);
using (var stream =
new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
return StatusCode(200);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}
My application is using AuthenticationStateProvider
for login process. But I don't know how to protect my upload route and allow access only for the current logged in user.