Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Web API Self-Host with Windows Authentication
    primarykey
    data
    text
    <p>I am trying to use the ASP.NET Web API Self-Host option with Windows authentication so I can determine the logged on user and ultimately accept or reject the user based on their identity. Here is my console application code:</p> <pre><code>using System; using System.Web.Http; using System.Web.Http.SelfHost; namespace SelfHost { class Program { static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://myComputerName:8080"); config.UseWindowsAuthentication = true; config.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); using (HttpSelfHostServer server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } } } } </code></pre> <p>Here is the controller:</p> <pre><code>[Authorize] public class HelloController : ApiController { public string Get() { // This next line throws an null reference exception if the Authorize // attribute is commented out. string userName = Request.GetUserPrincipal().Identity.Name; return "Hello " + userName; } } </code></pre> <p>Edit - I added the Authorize attribute, and the debugger shows that the code inside the Get action method is never invoked. The following HTML is returned:</p> <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt; &lt;HTML&gt;&lt;HEAD&gt; &lt;META content="text/html; charset=windows-1252" http-equiv=Content-Type&gt;&lt;/HEAD&gt; &lt;BODY&gt;&lt;/BODY&gt;&lt;/HTML&gt; </code></pre> <p>If the Authorize attribute is commented out, <code>Request.GetUserPrincipal().Identity.Name</code> throws a null reference exception since <code>Request.GetUserPrincipal()</code> yields null.</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.
 

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