我遇到了这个问题,我试图使用ServiceStack RedisAuthRepository来存储用户信息,这样我就可以使用它来重新水合嵌入JWT令牌中的用户会话。到目前为止,这是我的会议。
Plugins.Add(new AuthFeature(() => new PlatformSession(),
new IAuthProvider[] {
new JwtAuthProvider(AppSettings) {
ExpireTokensIn = TimeSpan.FromSeconds(120),
ExpireRefreshTokensIn = TimeSpan.FromDays(14),
AuthKeyBase64 = ConfigurationManager.AppSettings["jwt.AuthKeyBase64"],
CreatePayloadFilter = Payload.CreatePayloadFilter,
RequireSecureConnection = false
},
new CustomCredentialsAuthProvider()
}
));
var redisClientsManager = new RedisManagerPool("localhost:6379");
container.Register<ICacheClient>(c => c.Resolve<IRedisClientsManager>().GetCacheClient()).ReusedWithin(Funq.ReuseScope.None);
container.Register<IRedisClientsManager>(c => redisClientsManager);
container.Register<IAuthRepository>(c => new RedisAuthRepository(c.Resolve<IRedisClientsManager>()));
我的问题是,当我向用户进行身份验证时,它似乎不会在redis中保存任何内容。即使我添加了一个测试用户,也没有显示任何内容。
var authRepo = (IUserAuthRepository)HostContext.TryResolve<IAuthRepository>();
try
{
authRepo.CreateUserAuth(new User
{
UserName = "1",
UserId = 1,
ActiveCustomerId = 1
}, "test");
}
catch (Exception) {
编辑:已解决。在我的案例中,问题是分配给对象的用户名无效,这就是为什么它从未被存储。。。
RedisAuthRepository确实将UserAuth保存在Redis中,如Gistlyn:中的RedisSuthRepository示例所示
var redisManager = new RedisManagerPool("localhost:6379");
var redisAuth = new RedisAuthRepository(redisManager);
var id = new Random().Next(1,10000);
var userAuth = redisAuth.CreateUserAuth(new UserAuth
{
UserName = $"UserName{id}",
Email = $"test@email{id}.com",
DisplayName = "First Last",
}, "test");
var redis = redisManager.GetClient();
var fromRedis = redis.As<IUserAuth>().GetById(userAuth.Id);
fromRedis.PrintDump();
使用自定义UserAuth类型
如果您使用的是自定义UserAuth
模型,那么您也应该使用RedisAuthRepository<TUserAuth,TUserAuthDetails>
的通用版本以及自定义UserAuth、UserAuthDetails表的类型,例如:
集装箱。寄存器(c=>新建RedisAuthRepository(c.Resolve());