Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem of losing the principal on a new thread is mentioned here:</p> <p><a href="http://leastprivilege.com/2012/06/25/important-setting-the-client-principal-in-asp-net-web-api/">http://leastprivilege.com/2012/06/25/important-setting-the-client-principal-in-asp-net-web-api/</a></p> <blockquote> <p><strong>Important: Setting the Client Principal in ASP.NET Web API</strong></p> <p>Due to some unfortunate mechanisms buried deep in ASP.NET, setting Thread.CurrentPrincipal in Web API web hosting is not enough.</p> <p>When hosting in ASP.NET, Thread.CurrentPrincipal might get overridden with HttpContext.Current.User when creating new threads. This means you have to set the principal on both the thread and the HTTP context.</p> </blockquote> <p>And here: <a href="http://aspnetwebstack.codeplex.com/workitem/264">http://aspnetwebstack.codeplex.com/workitem/264</a></p> <blockquote> <p>Today, you will need to set both of the following for user principal if you use a custom message handler to perform authentication in the web hosted scenario.</p> <pre><code>IPrincipal principal = new GenericPrincipal( new GenericIdentity("myuser"), new string[] { "myrole" }); Thread.CurrentPrincipal = principal; HttpContext.Current.User = principal; </code></pre> </blockquote> <p>I have added the last line <code>HttpContext.Current.User = principal</code> (needs <code>using System.Web;</code>) to the message handler and the <code>User</code> property in the <code>ApiController</code> does always have the correct principal now, even if the thread has changed due to the task in the MediaTypeFormatter.</p> <p><strong>Edit</strong></p> <p>Just to emphasize it: Setting the current user's principal of the <code>HttpContext</code> is only necessary when the WebApi is hosted in ASP.NET/IIS. For self-hosting it is not necessary (and not possible because <code>HttpContext</code> is an ASP.NET construct and doesn't exist when self hosted).</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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