gpt4 book ai didi

mysql - 当遇到 my_aspnet_* 表时迁移到 ServiceStack 身份验证框架的最佳方法是什么

转载 作者:行者123 更新时间:2023-11-29 01:54:40 25 4
gpt4 key购买 nike

我还没有完全准备好从 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;
}
}

最佳答案

与现有用户身份验证表集成

如果您想使用现有的 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 User Auth 表中,你将配置为在你的 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);
}
}

在此之后,您将从旧的用户信息中获得新的用户帐户,您可以使用这些帐户登录。

关于mysql - 当遇到 my_aspnet_* 表时迁移到 ServiceStack 身份验证框架的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com