title: ServiceStack Custom Credentials Auth with DB Stored Api Keys
body: <p>Right now, we're authenticating our users with this:</p>
  
  <pre><code>public class WindowsAuthProvider : CredentialsAuthProvider
  {
      public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
      {
          using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "OurDomain"))
          {
              // TODO make sure user record exists in custom DB tables as well
              return pc.ValidateCredentials(userName, password);
          }
      }
  
      public override IHttpResult OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary&lt;string, string&gt; authInfo)
      {
          return base.OnAuthenticated(authService, session, tokens, authInfo);
      }
  }
  </code></pre>
  
  <p>Which works great when using the JsonServiceClient.</p>
  
  <p>We have some legacy code written in Visual FoxPro which wants to call some of the authenticated functions in ServiceStack... to accommodate this, we'd like to also allow Api Keys. We want the API Keys to be stored in SQL Server to avoid issues if the process stops / restarts. So, the client would authenticate with domain credentials, then generate an API key for subsequent calls which would be stored in the database (ideally just using the table servicestack can create (dbo.ApiKey).</p>
  
  <p>If we were to set this per the docs:</p>
  
  <pre><code>container.Register&lt;IAuthRepository&gt;(c =&gt; new OrmLiteAuthRepository(dbFactory));
  </code></pre>
  
  <p>We get an error on the <code>OnAuthenticated</code> function above telling us we should call <code>Init()</code>... like its trying to also create the user tables. So I'm not sure how to allow DB stored API Keys, along with custom authentication that relies on both active directory as well as our custom tables for users and roles.</p>
  
  <p>Instead of inheriting from CredentialsAuthProvider, maybe its better to register a custom <code>IUserAuthRepository</code> and <code>IManageRoles</code>?</p>