In the ApiKey authentication documentation, it mentions that ApiKey records are automatically created for new users. This isn’t happening for me. Are they created in CreateUserAuth?
Here is our AppHost configuration:
//Tell ServiceStack you want to persist User Info in the registered RDBMS
container.Register<IUserAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>())
{
UseDistinctRoleTables = false
});
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new CustomCredentialsAuthProvider(),
new ApiKeyAuthProvider() {
RequireSecureConnection = false
}
},
htmlRedirect: "~/"
));
Moreover, when browsing the source code in GitHub, I can’t find where this actually happens. Is the documentation incorrect? We are using 4.5.0. Can anyone point me in the right direction?
For some reasons this is not triggering the AuthEvents on the AuthFeature.
My solution, for now, is to Register the ApiKeyAuthProvider and then generate the keys myself:
var apiKeys = ApiKeyAuthProvider.GenerateNewApiKeys(authUser.Id.ToString());
var authRepo = (IManageApiKeys)HostContext.AppHost.GetAuthRepository(Request);
authRepo.StoreAll(apiKeys);
But something tells me this is not the correct way. Any feedback?
CreateUserAuth() is just an API on the IAuthRepository, a clean API abstraction used by the AuthFeature to persisting users in data stores, it doesn’t itself have any knowledge or coupling to either the AuthFeature, current HTTP Request, etc. Auth Filters themselves are only executed within the context of an Auth Request.