[英]What's the best way to migrate to ServiceStack authentication framework when stuck with my_aspnet_* tables
我還沒有准備好從MySQL用戶/角色/配置文件提供程序格式更改我的所有用戶/身份驗證表,但是正在將MVC移到ServiceStack。
是否可以在某個地方使用預構建的IUserAuthRespository和/或CredentialsAuthProvider,還是需要構建一個預構建的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;
}
}
如果要使用現有的User / Auth表,最簡單的解決方案是忽略UserAuth存儲庫,並實現一個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用戶身份驗證表,則可以配置為在OrmLiteAuthRepository
中使用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 DB連接從現有表中進行選擇,然后使用IUserAuthRepository
創建新的Users。
// 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);
}
}
之后,您將使用舊的用戶信息中的新用戶帳戶登錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.