Note that there are some explanatory texts on larger screens.

plurals
  1. POWebApi: User.Identity.IsAuthenticated true for first request, false thereafter
    primarykey
    data
    text
    <p>I've implemented a Web API.</p> <pre><code>&lt;authentication mode="None" /&gt; </code></pre> <p>I'm using Basic authorization, and set the Thread.CurrentPrincipal in my AuthorizeAttribute.</p> <p>The first time after starting/debugging the application, I submit a request, set the Thread.CurrentPrincipal (with IsAuthenticated = true) server-side, and IsAuthenticated returns true in my controller, as expected. Any request after this, however, sets the Thread.CurrentPrincipal as normal, but by time the execution hits my controller methods, the controllers' User.Identity property has been changed, and IsAuthenticated = false.</p> <p>I can't figure out why IsAuthenticated=true for the first time after starting the application only?! It should be every time, as I'm setting the Thread.CurrentPrinciple manually, but somewhere between there and hitting my controller, it is being replaced!</p> <p><strong>UPDATE</strong></p> <p>It's something to do with a MediaTypeFormatter that I've added. When I remove the formatter, I don't get the issue. The formatter's code that gets executed is below:</p> <pre><code>public override Task&lt;object&gt; ReadFromStreamAsync(Type type, System.IO.Stream webStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger) { return Task.Factory.StartNew(() =&gt; { string temporaryFilePath = Path.Combine(TemporaryDirectory, Path.GetRandomFileName()); using (FileStream fileStream = new FileStream(temporaryFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.Read)) { webStream.CopyTo(fileStream); } return (object)new CustomFile(temporaryFilePath); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload