I’m trying to add basic auth to my API and I went through the documentation at:
https://docs.servicestack.net/authentication-and-authorization
I used x mix auth-db mysql which added to my project:
Configure.Db.cs and
Configure.AuthRepository.cs
The connection to mysql works and using the generated code:
“MyTable” is created with 3 other tables:
appuser
userauthdetails
userauthrole
My first question is: What is supposed to be the role of “MyTable”? Is it used to stored custom information about a user etc.?
Also using the sample code generated in:
Configure.AuthRepository.cs
public void Configure(IAppHost appHost)
{
var authRepo = appHost.Resolve<IAuthRepository>();
authRepo.InitSchema();
CreateUser(authRepo, "admin@email.com", "Admin User", "p@55wOrd", roles:new[]{ RoleNames.Admin });
}
// Add initial Users to the configured Auth Repository
public void CreateUser(IAuthRepository authRepo, string email, string name, string password, string[] roles)
{
if (authRepo.GetUserAuthByUserName(email) == null)
{
var newAdmin = new AppUser { Email = email, DisplayName = name };
var user = authRepo.CreateUserAuth(newAdmin, password);
authRepo.AssignRoles(user, roles);
}
}
Doesn’t seem to create a username in the table appuser and the Roles column is empty?
Is this the expected behaviour for the code above? I would have expected:
authRepo.AssignRoles(user, roles);
To create the roles…?
Am I missing a step somewhere…?
Of course, trying to authenticate using the JsonServiceClient returns Unauthorized:
var authResponse = client.Post(new Authenticate {
UserName = "admin@email.com",
Password = "p@55w0rd",
RememberMe = true,
});
Here is also my AppHost in case it helps.
public class AppHost : AppHostBase
{
public AppHost()
: base("SimpleApi", typeof(CallServices).Assembly) { }
public override void Configure(Container container)
{
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new BasicAuthProvider()
}));
Plugins.Add(new RegistrationFeature());
Feature disableFeatures = Feature.Jsv;
SetConfig(new HostConfig {
EnableFeatures = Feature.All.Remove(disableFeatures),
DebugMode = false,
WriteErrorsToResponse = false
});
}
}
Thanks!