I have ASP.NET Core WebApi application and need to upload very large file. I have enabled JWT Auth/Authz
services.AddAuthentication(cfg =>
{
cfg.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
cfg.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, cfg =>
{ .. }
TTL of token is 2 minutes.
Then I have action in my controller like
public async Task<IActionResult> Add(IFormFile file)
{
if (file.Length.ToMegabytes() > 500)
{
throw new BadRequestException(nameof(file), "File size should not exceed 500 MB");
}
}
and config in web host
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
})
When I upload a large file (~1Gb) it is take a long time and then returns 401 before it step into my action.
How can I check that file has not valid size before it will be uploaded? Because uploading take a time more than 2 minutes and in a result I will get 401