title: Asp.net web API 2 authentication with asp.net Identity
body: <p>I have Solution consisting of 3 projects, first is Data Access Layer (where models and Database Context are defined), and other two are Asp.net MVC 5 and Asp.net Web Api 2 projects.</p>
  
  <p>For authentication and authorization I'm using Asp.net Identity which I've set up like this (in my DAL project):</p>
  
  <pre><code>public class DatabaseContext : IdentityDbContext&lt;User&gt;
  {
      public DatabaseContext()
          : base("DefaultConnection")
      {
          Configuration.LazyLoadingEnabled = true;
      }
  
      public DbSet&lt;IdentityUser&gt; IdentityUsers { get; set; }
  
      /*
          Other DbSets ...
      */
  }
  </code></pre>
  
  <p>My User class extends IdentityUser.</p>
  
  <p>In my ASP.net project I've changed a bit Account controller so it works with my Database Context, here are relevant parts which I changed:</p>
  
  <pre><code>[Authorize]
  public class AccountController : Controller
  {
      public AccountController()
          : this(new UserManager&lt;User&gt;(new UserStore&lt;User&gt;(new DatabaseContext())))
      {
      }
  
      public AccountController(UserManager&lt;User&gt; userManager)
      {
          UserManager = userManager;
      }
  
      public UserManager&lt;User&gt; UserManager { get; private set; }
  }
  </code></pre>
  
  <p>And this part works fine. So my question is, what changes I have to do in order my Web API project works with my entity User and my Database Context, so in order to consume API user must be logged in ? (and of-course user has option to register via API). </p>
  
  <p>I.E. I want to enable authorization and authentication for web api 2 by using my Database Context and class User which extends Identity User class.  </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>What I've tried to do is this: I've replaced all IdentityUser classes in my web api 2 project with my class User, but on line (when I try to log in):</p>
  
  <pre><code>User user =  await userManager.FindAsync(context.UserName, context.Password);
  </code></pre>
  
  <p>I get error:</p>
  
  <p>System.Data.SqlClient.SqlException: Invalid object name 'dbo.AspNetUsers'.</p>
  
  <p>This approach worked for my asp.net MVC project.</p>