title: How to retrieve files from (jquery) Asp.Net webApi, authorized with Asp.Net Identity
body: <p>Maybe there is a really simple solution... But lot of threads I looked at did not solve my issue... I will try to be as clear as possible.</p>
  
  <p>On a Asp.Net WebApi project there is "DownloadController" which is currently looking like: </p>
  
  <pre><code>private HttpResponseMessage CreateFileResponse(string fileName, string originalName, List&lt;string&gt; allowedMimes)
      {
          // see if file exists
          if (!_FileProvider.Exists(fileName))
          {
              return Request.CreateResponse(HttpStatusCode.NotFound);
          }
  
          // retrieve mimeType
          string mimeType = MimeMapping.GetMimeMapping(fileName);
          if (!allowedMimes.Contains(mimeType))
          {
              mimeType = _generalMimeType;
          }
  
          // open the file
          FileStream fileStream = _FileProvider.Open(fileName);
  
          // create the response
          var response = new HttpResponseMessage();
          response.Content = new StreamContent(fileStream);
          response.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
          response.Content.Headers.ContentLength = _FileProvider.GetLength(fileName);
  
          // if the mimetype is not allowed, download as attachment
          if (mimeType == _generalMimeType)
          {
              response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
              response.Content.Headers.ContentDisposition.FileName = originalName;
          }
  
          return response;
      }
  
      //for understanding the above
      public DownloadController()
      {
          _FileProvider = new FileProvider();
      }
  
      public interface IFileProvider
      {
          bool Exists(string name);
          FileStream Open(string name);
          long GetLength(string name);
      }
  </code></pre>
  
  <p>This piece of code worked perfectly when a simple HTTP GET (just an url in the browser) is done to a certain route on the controller.</p>
  
  <pre><code>    [Route("Download/Document/{documentid}")]
      public HttpResponseMessage GetDocument(Guid documentid)
      {
         -
         -
         -
         -
     return this.CreateFileResponse(fileName, document.filename, allowedMimes);    
      }
  </code></pre>
  
  <p>But now all controllers all authorized with the [Authorize] field of asp.net Identity. All requests now need a Authorization field with a token (Bearer "token") in there header. Now almost all requests are done with jquery where you can simply add a header field. But for downloading files this was not done. Using jquery with a token header on the DownloadController will give a response though:  </p>
  
  <blockquote>
    <p>�PNG ↵↵ IHDR� ��sRGB���gAMA���a   pHYs���o........</p>
  </blockquote>
  
  <p>Also fiddler and Postman are capable of showing the real image when I make a request with a token. (don't know for pdf files or other files)</p>
  
  <p>But setting this data with certain javascript solutions I tried on stackoverflow won't work.... The image keeps broken.. Can somebody help me with a solution to set this data from javascript?:). Or an other solution of course.</p>
  
  <p>Intercepted response from Fiddler:
  <a href="https://i.stack.imgur.com/VFzVy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VFzVy.png" alt="Intercepted response from Fiddler"></a></p>