title: ASP.NET MVC5/AngularJS/Web API app using Windows Authentication and OWIN
body: <p>I may be over-complicating things, but we have an internal ASP.NET MVC5 SPA with AngularJS using Windows Authentication.  This application has a SQL back-end database that has a table of users, containing their account names, and their respective roles in the application.  We will be making calls to another Web API application that also has Windows Authentication enabled.  </p>
  
  <p>I have tried to do research on how to handle authorization using OWIN but couldn't find any specific examples regarding OWIN and Windows Authentication.  Everything that turns up uses forms authentication with a username and password.</p>
  
  <p>How can I go about using OWIN and Windows Auth for my app?  Here's a sample of my OAuthAuthorizationServerProvider class.  </p>
  
  <pre><code>public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
  {
      public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
      {
          context.Validated();
          return;
      }
  
      public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
      {
          context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
  
          var container = UnityHelper.GetContainerInstance("***");
          var securityHelper = container.Resolve&lt;ISecurityHelper&gt;();
  
          User currentUser = securityHelper.GetCurrentUser(); // Validates user based on HttpContext.Current.User
          if (currentUser == null)
          {
              context.SetError("invalid_grant", "The user could not be found.");
              return;
          }
  
          var identity = new ClaimsIdentity(context.Options.AuthenticationType);
          identity.AddClaim(new Claim("sub", currentUser.AccountName));
          identity.AddClaim(new Claim("role", "user"));
  
          context.Validated(identity);
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  Oops, I forgot to include more information on what we'd like to accomplish.  If possible, we'd like to use bearer authentication tickets so we don't have to look up the user and their roles everytime we make a call to a web api method.</p>
  
  <p><strong>UPDATE 2:</strong>
  As requested by Andrew, below is the TLDR version of my _securityHelper class, specifically the GetCurrentUser() method.  You'll notice that I'm attempting to call:</p>
  
  <pre><code>HttpContext.Current.GetOwinContext().Request.User.Identity.Name
  </code></pre>
  
  <p>This always returns <em><strong>null</strong></em> for User.</p>
  
  <pre><code>public class SecurityHelper : ISecurityHelper
  {
      private readonly ISecurityGroupController _securityGroupController;
      private readonly IUserController _userController;
      private readonly IEmployeeController _employeeController;
      private readonly IFieldPermissionController _fieldPermissionController;
      private readonly IOACController _oacController;
  
      public SecurityHelper(ISecurityGroupController securityGroupController,
          IUserController userController,
          IEmployeeController employeeController,
          IFieldPermissionController fieldPermissionController,
          IOACController oacController)
      {
          _securityGroupController = securityGroupController;
          _userController = userController;
          _employeeController = employeeController;
          _fieldPermissionController = fieldPermissionController;
          _oacController = oacController;
      }
  
      // ... other methods
  
      public User GetCurrentUser()
      {
          User user = _userController.GetByAccountName(HttpContext.Current.GetOwinContext().Request.User.Identity.Name);
          if (user != null)
          {
              List&lt;OAC&gt; memberships = _oacController.GetMemberships(user.SourceId).ToList();
              if (IsTestModeEnabled() &amp;&amp; ((user.OACMemberships != null &amp;&amp; user.OACMemberships.Count == 0) || user.OACMemberships == null))
              {
                  user.OACMemberships = memberships;
              }
              else if (!IsTestModeEnabled())
              {
                  user.OACMemberships = memberships;
              }
          }
  
          return user;
      }
  }
  </code></pre>