title: Best way to validate files in ASP .NET Core Controller?
body: <p>I have an Angular application with .NET Core backend.</p>
  <p>To perform a bulk import users can upload a file with multiple lines. Each line contains an email adress which then gets invited to create an account for the application.</p>
  <p>For that I have an input-Field of type file.</p>
  <pre><code>&lt;input type=&quot;file&quot;&gt;
  </code></pre>
  <p>The file gets sent to my backend controller as <code>multipart/form-data</code>.</p>
  <p>Now what is the best way to validate the file?</p>
  <p>Currently, I'm checking the ContentType against a list of allowed ContentTypes. Furthermore, I check the size of the file because a text file with the estimated content should not use more than 250 KB.</p>
  <pre><code>var allowedFileTypes = new List&lt;string&gt;
    {
      &quot;.txt&quot;,
      &quot;.csv&quot;,
      &quot;text/comma-separated-values&quot;,
      &quot;text/plain&quot;,
      &quot;text/richtext&quot;,
      &quot;text/rtf&quot;,
      &quot;application/vnd.ms-excel&quot;
    };
  
    var maxFileSize = 250000; //250KB
  
    var file = Request.Form.Files.First();
  
    if (!allowedFileTypes.Contains(file.ContentType))
      return BadRequest();
  
    if (file.Length &gt; maxFileSize)
      return BadRequest();
  </code></pre>
  <p>Are there any other ways how I can validate the file? Are there any security concerns I have to think of? Is it for example possible to inject malicious code inside of an .csv file.</p>