ServiceStack RedisAuthRepository不存储任何内容

时间:2017-10-24 12:04:44

标签: servicestack refresh-token

我遇到这个问题,我正在尝试使用ServiceStack RedisAuthRepository存储用户信息,以便我可以使用它来重新融合嵌入在JWT令牌中的用户会话。到目前为止,这是我的conf。

   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) { 

编辑: 解决了。 我的问题是分配给该对象的用户名无效,这就是为什么它从未存储过......

1 个答案:

答案 0 :(得分:1)

RedisAuthRepository确实在Redis中保存UserAuth,如RedisAuthRepository example in Gistlyn所示:

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表的类型,例如:

container.Register(c =&gt;     新的RedisAuthRepository(         c.Resolve()));