Configuring ASP.Net Identity for an ASP.Net WebAPI application
<p>I have a SPA ASP.NET WebAPI application which previously allowed anonymous access. I have now configured ASP.Net Identity for it but I cannot get the Identity related controllers and my application's other controllers to work at the same time :-( 
It's either one or the other!</p>

<p>I have added the startup class to my project:</p>

<pre><code>using Test.MyProject;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataHandler.Encoder;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;
using Owin;
using System;
using System.Configuration;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http;

[assembly: OwinStartup(typeof(Test.Client.Startup))]
namespace Test.Client
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration httpConfig = new HttpConfiguration();

            ConfigureOAuthTokenGeneration(app);

            ConfigureWebApi(httpConfig);

            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

            //app.UseWebApi(httpConfig);  // If this line is commented out my application's controllers work. But then my Account Controller does't work. It if is included, my application's controllers don't work, whilst the Account Controller work

            GlobalConfiguration.Configure(WebApiConfig.Register);
        }

        private void ConfigureOAuthTokenGeneration(IAppBuilder app)
        {
            // Configure the db context and user manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext&lt;ApplicationUserManager&gt;(ApplicationUserManager.Create);
        }

        private void ConfigureWebApi(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            var jsonFormatter = config.Formatters.OfType&lt;JsonMediaTypeFormatter&gt;().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }
    }
}
</code></pre>

<p>And I have added controllers for managing Users and Roles.
The statement <code>GlobalConfiguration.Configure(WebApiConfig.Register)</code> was previously in the application start event in <code>global.aspx.cs</code> but now moved to the startup class to have everything in the same place.</p>

<p>The <code>WebApiConfig.Register</code> method looks like this:</p>

<pre><code>public static void Register(HttpConfiguration config)
{
    var container = new UnityContainer();
    // Web API configuration and services

    string appStorageProvider = ConfigurationManager.AppSettings["StorageProvider"];
    var provider =(TestComposition.StorageProvider) Enum.Parse(typeof (TestComposition.StorageProvider), appStorageProvider, true);

    TestComposition.Setup(container, provider);
    container.RegisterType&lt;GeneralLogger, GeneralLogger&gt;();

    container.RegisterType&lt;IExceptionLogger, ExceptionLogger&gt;();

    config.EnableCors();

    config.DependencyResolver = new UnityResolver(container);
    config.Services.Add(typeof (IExceptionLogger), container.Resolve&lt;GeneralLogger&gt;());

    // Web API routes
    config.MapHttpAttributeRoutes();
}
</code></pre>

<p>In my new <code>AccountController</code> I have code which allows me to retrieve the <code>ApplicationUserManager</code> from the <code>OwinContext</code> set up in the Startup class.</p>

<pre><code>protected ApplicationUserManager AppUserManager
{
    get
    {
        return _AppUserManager ?? Request.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
    }
}
</code></pre>

<p>With <code>app.UseWebApi(httpConfig)</code> commented out as shown above my application works as it used to. But if I invoke any action on my new <code>AccountController</code> I get this:</p>

<blockquote>
  <p>Request.GetOwinContext()  error CS1061: 'HttpRequestMessage' does not
  contain a definition for 'GetOwinContext' and no extension method
  'GetOwinContext' accepting a first argument of type
  'HttpRequestMessage' could be found (are you missing a using directive
  or an assembly reference?)</p>
</blockquote>

<p>If I comment in the <code>app.UseWebApi(httpConfig)</code> statement the <code>AccountController</code> works but then my other controllers don't work. Here I get errors like these:</p>

<blockquote>
  <p>{
      "message": "An error has occurred.",
      "exceptionMessage": "An error occurred when trying to create a controller of type 'TestController'. Make sure that the controller has
  a parameterless public constructor.",
      "exceptionType": "System.InvalidOperationException",
      "stackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage
  request, HttpControllerDescriptor controllerDescriptor, Type
  controllerType)\r\n   at
  System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage
  request)\r\n   at
  System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()",
      "innerException": {
          "message": "An error has occurred.",
          "exceptionMessage": "Type 'MyProject.Api.TestController' does not have a default constructor",
          "exceptionType": "System.ArgumentException",
          "stackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at
  System.Web.Http.Internal.TypeActivator.Create[TBase](Type
  instanceType)\r\n   at
  System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage
  request, Type controllerType, Func`1&amp; activator)\r\n   at
  System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage
  request, HttpControllerDescriptor controllerDescriptor, Type
  controllerType)"
      } }</p>
</blockquote>

<p>Any idea what is going on here?</p>