title: Where is the appropriate spot to seed and login a default user with ASP.NET MVC Identity 2.0?
body: <p>I have an application that uses Identity 2.0 with Owin.</p>
  <p>I want the application to seed a default user and log in automatically while on development (I'm wrapping the method and its call with <code>#if DEBUG</code> condition).</p>
  <p>I tried adding the following method to <em>IdentityConfig.cs</em><code>ApplicationUserManager.Create</code> method , but after calling it, <code>HttpContext.Current.User</code> is still null at the next call.</p>
  <pre><code>private void SeedAndLoginDefaultUser()
  {
    var user = this.FindByName(&quot;***&quot;);
    if (user == null)
      this.Create(new ApplicationUser { /* ... */ }, &quot;***&quot;);
  
    using (var signInManager = new ApplicationSignInManager(this, 
        HttpContext.Current.GetOwinContext().Authentication))
      signInManager.PasswordSignIn(user.UserName, &quot;***&quot;, true, false);
  }
  </code></pre>
  <p>But it keeps on authenticating the user over and over. I'm sure there should be a more appropriate location to perform this automation.<br />
  P.S. doing it from the <em>Global.asax.cs</em> or <em>Startup.cs</em> is impossible, since owin isn't initialized yet at that stage.</p>
  <p>I tried adding it in the the Context initializer, the user gets created, but the login fails.</p>
  <h2>Update</h2>
  <p>on <a href="https://stackoverflow.com/a/29133560/75500">this</a> answer:</p>
  <p>Actually the UserManager is anyway overridden in the <em>IdentityConfig.cs</em> class. But what I'm thinking is, your solution will check for user existence each time a method attributed under <code>CustomAuthorize</code> is called. Whereas I only want to perform the default user creation and login only at app startup.</p>
  <h2>Update 2</h2>
  <p>In my database initializer I have some crucial seed tables, that has data related to user, which means, I have to create the user in the seed method. So my question is now, when I create a user manually as the following code:</p>
  <pre><code>public class Initializer : DropCreateDatabaseAlways&lt;ApplicationDbContext&gt;
  {
    private static volatile bool processing;
    protected override void Seed(ApplicationDbContext context)
    {
      if (processing)
        return;
      processing = true;
      using (var um = ApplicationUserManager.Get())
      {
        var shimmy = new ApplicationUser
         {
           //Id = Guid.NewGuid().ToString(),
           FirstName = &quot;Shimmy&quot;,
           LastName = &quot;Weitzhandler&quot;,
           UserName = &quot;***&quot;,
           Email = &quot;***&quot;,
           //EmailConfirmed = true,
         };
        var task = um.CreateAsync(shimmy, &quot;***&quot;);
        task.Wait();
        var result = task.Result;
  
        if (result.Succeeded)
        {
          using (var sm = ApplicationSignInManager.Get())
          {
            var siTask = sm.SignInAsync(shimmy, true, true);
            siTask.Wait();
            if (siTask.Exception != null)
            {
              throw siTask.Exception;
            }
          }
        }
        else
        {
          throw new InvalidOperationException();
        }
      }
  
      //seed my tables
      processing = false;
    }
  }
  </code></pre>
  <p>As you can see I even added a static member to avoid looping back the initializer, but I believe it still doesn't help.
  Because when I'm trying to create the user manually (adding to the <code>Users</code> table, using the <code>UserManager.PasswordHasher</code>, I then can't log in to the account.
  But the problem with the code above is that I'm calling it from the <code>Seed</code>, and I suspect it calls itself back internally when I try to create a user, then it gets stuck.
  The resolution I'm looking for is how to create a user manually and be able to login to that account. I'm probably missing some point in the password hashing / user verification / whatever else.</p>