我还没准备好从MySQL用户/角色/配置文件提供程序格式更改我的所有用户/身份验证表,但是从MVC转移到ServiceStack。
是否有可以使用的预构建的IUserAuthRespository和/或CredentialsAuthProvider,或者我是否需要构建一个来提供此映射?
如果我需要构建一个,我假设在IUserAuthRepository级别实现是最干净的?是否有实现基本登录/注销(以及管理“切换用户”模拟)功能所需的最少方法?
我尝试实现自定义CredentialsAuthProvider,这似乎有效,但我无法获取本地帖子以模仿使用正确的提供程序。寻找解决方案,我意识到可能更好地实现存储库。
编辑: 我目前注册的自定义身份验证提供程序是:
Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[]
{
container.Resolve<MySqlCredentialsAuthProvider>() //HTML Form post of UserName/Password credentials
}));
调用AuthenticateService的本地帖子的代码是:
[RequiredRole(SystemRoles.Administrator)]
public object Any(ImpersonateUser request)
{
using (var service = base.ResolveService<AuthenticateService>()) //In Process
{
//lets us login without a password if we call it internally
var result = service.Post(new Authenticate
{
provider = AuthenticateService.CredentialsProvider,
UserName = request.Username,
//Password = "should-not-matter-since-we-are-posting-locally"
});
return result;
}
}
答案 0 :(得分:3)
如果要使用现有的User / Auth表,最简单的解决方案是忽略UserAuth存储库和查看现有数据库表的implement a Custom CredentialsAuthProvider,以返回其身份验证尝试是否成功。
实施OnAuthenticated()
以填充数据库中其他类型的IAuthSession
,例如:
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService,
string userName, string password)
{
//Add here your custom auth logic (database calls etc)
//Return true if credentials are valid, otherwise false
}
public override IHttpResult OnAuthenticated(IServiceBase authService,
IAuthSession session, IAuthTokens tokens,
Dictionary<string, string> authInfo)
{
//Fill IAuthSession with data you want to retrieve in the app eg:
session.FirstName = "some_firstname_from_db";
//...
//Call base method to Save Session and fire Auth/Session callbacks:
return base.OnAuthenticated(authService, session, tokens, authInfo);
//Alternatively avoid built-in behavior and explicitly save session with
//authService.SaveSession(session, SessionExpiry);
//return null;
}
}
如果要将它们导入OrmLite用户身份验证表,您可以配置为在AppHost中使用OrmLiteAuthRepository
:
//Register to use MySql Dialect Provider
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(dbConnString, MySqlDialect.Provider));
Plugins.Add(new AuthFeature(
() => new CustomUserSession(), //Use your own typed Custom UserSession type
new IAuthProvider[] {
//HTML Form post of UserName/Password credentials
new CredentialsAuthProvider()
}));
//Tell ServiceStack you want to persist User Info in the registered MySql DB above
container.Register<IUserAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
//Resolve instance of configured IUserAuthRepository
var userAuth = container.Resolve<IUserAuthRepository>();
//Create any missing UserAuth RDBMS tables
authRepo.InitSchema();
然后要导入数据,您可以使用上面的MySQL数据库连接从现有表中进行选择,然后使用IUserAuthRepository
创建新用户。
// Open DB Connection to RDBMS
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
//Example of fetching old Users out of a custom table (use your table instead)
var oldUsers = db.Select<OldUserInfo>();
// Clear existing UserAuth tables if you want to replay this import
//db.DeleteAll<UserAuthDetails>();
//db.DeleteAll<UserAuth>();
//Go through and create new User Accounts using Old User Info
foreach (var oldUser in oldUsers)
{
//Create New User Info from Old Info
var newUser = new UserAuth {
UserName = oldUser.UserName,
Email = oldUser.Email,
//...
};
//Create New User Account with oldUser Password
authRepo.CreateUserAuth(newUser, oldUser.Password);
}
}
在此之后,您将从旧用户信息中获得可以登录的新用户帐户。