title: API keys remain empty
body: <p>I refer to <a href="https://stackoverflow.com/questions/43292099/authenticate-server-to-server-communication-with-api-key/43292283#43292283">this question</a>. I try to create a server account if it does not exist when the server starts. I have overwritten <code>OnAfterInit</code> to do this. Unfortunately no keys are created and worse, my ApiKeyAuthProvider seems to be null! Here is the code:</p>
  
  <pre><code>public class AppHost : AppHostHttpListenerBase
  {
      private PooledRedisClientManager RedisBusPool { get; set; }
  
      public override void Configure(Container container)
      {
          RedisBusPool = new PooledRedisClientManager(connStrBus);
  
          #region IOC registrations
          container.Register&lt;IRedisClientsManager&gt;(c =&gt; new PooledRedisClientManager(connStrBus));
          container.Register(c =&gt; new RedisAuthRepository(RedisBusPool));
          //...
          #endregion
  
  
          #region ServiceStack plugins
          Plugins.Add(new AuthFeature(() =&gt; new AuthUserSession(), 
              new IAuthProvider[] {
                  new BediCredentialsAuthProvider(),
                  new ApiKeyAuthProvider(AppSettings)
                  {
                      KeyTypes = new []{"secret", "publishable"},
                  },
              }
          ));
          //...
  
          #endregion
          //...
      }
  
      public override void OnAfterInit()
      {
          #region Add test users
          var authRepo = TryResolve&lt;RedisAuthRepository&gt;();
          var user = authRepo.GetUserAuthByUserName("JoeTest");
          if (user == null)
          {
              var newUser = new UserAuth();
              newUser.UserName = "JoeTest";
              newUser.Email = "Joe@test.com";
              newUser.FirstName = "Joe";
              newUser.LastName = "Test";
              user = authRepo.CreateUserAuth(newUser, "topSecret!");
          }
  
          var keys = authRepo.GetUserApiKeys(user.Id.ToString());
          foreach (var apiKey in keys)
          {
              Debug.WriteLine(apiKey);
          }
  
          //CANT GET THE AUTHPROVIDER IT IS ALWAYS NULL HERE
          var authProvider = (ApiKeyAuthProvider)AuthenticateService.GetAuthProvider(ApiKeyAuthProvider.Name);
  
  
          #endregion
          base.OnAfterInit();
      }
  
  }
  </code></pre>
  
  <p>The user is successfully created with password, salt etc. Here is what my Redis database shows:</p>
  
  <pre><code>{
    "__type": "ServiceStack.Auth.UserAuth, ServiceStack",
    "Id": 2,
    "UserName": "JoeTest",
    "Email": "Joe@test.com",
    "FirstName": "Joe",
    "LastName": "Test",
    "Salt": "1/696g==",
    "PasswordHash": "FZNI5cR5TWTpbvvm9PDy/w/JdRZImQYU5m5P6z8j7TY=",
    "DigestHa1Hash": "e4e0cff88386c6f991d13a3802ce7b53",
    "Roles": [],
    "Permissions": [],
    "CreatedDate": "\/Date(1491811177540)\/",
    "ModifiedDate": "\/Date(1491811177540)\/",
    "InvalidLoginAttempts": 0
  }
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>How are the API key created? What am I missing or does this only work with the <code>RegistrationFeature</code>enabled? If so, how can I protect this (self-registration is not allowed in my scenario) and how can I use it after the initialization of my service?</li>
  <li><p>I was browsing a bit through the code of RedisAuthRepository.cs and found this:</p>
  
  <p><code>var saltedHash = HostContext.Resolve&lt;IHashProvider&gt;();</code></p></li>
  </ol>
  
  <p>Does this mean that I can implement and plug-in my own HashProvider? If so how does that work?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I figured out that I can assign keys but only if I put the code into <code>AfterInitCallbacks.Add</code>. It seems to be called AFTER <code>OnAfterInit()</code>.</p>
  
  <p>So this code creates the key for the account generated during ServiceStartup:</p>
  
  <pre><code>AfterInitCallbacks.Add(host =&gt;
  {
  
      var authProvider = (ApiKeyAuthProvider)AuthenticateService.GetAuthProvider(ApiKeyAuthProvider.Name);
      var authRepo = (IManageApiKeys)TryResolve&lt;RedisAuthRepository&gt;();
      var userRepo = (IUserAuthRepository)TryResolve&lt;RedisAuthRepository&gt;();
      var user = userRepo.GetUserAuthByUserName("JoeTest");
  
      var keys = authRepo.GetUserApiKeys(user.Id.ToString());
      if (keys.Count == 0)
      {
          try
          {
              keys = authProvider.GenerateNewApiKeys(user.Id.ToString());
              authRepo.StoreAll(keys);
  
          }
          catch (Exception e)
          {
              Debug.WriteLine(e);
              throw;
          }
      }
  });
  </code></pre>
  
  <p>At least I can see what is generated on Redis now. However it remains unclear to me <strong>how keys can be created automatically</strong>.</p>