我如何使用ServiceStack注册功能插件与Redis

本文关键字:插件 Redis 功能 注册 何使用 ServiceStack | 更新日期: 2023-09-27 18:13:23

我正在添加

Plugins.Add(new RegistrationFeature());

所以现在我已经在API页面注册服务,但我怎么能使用它与redis?有什么简单的方法吗?

public object Post(Register register)
{
    var Id = DBService.RegisterUser(register.UserName, register.FirstName, register.LastName, register.DisplayName, register.Email, register.Password, register.AutoLogin, register.Continue);

and Redis page:

public string RegisterUser(string userName, string firstName, string lastName, string displayName, string email,
                           string password, bool? autoLogin, string @continue)
{
    using (var redisclient = RedisManager.GetClient())
    {
        var redisUser = redisclient.As<Register>();
        var yeniKayit = new Register()
        {
            AutoLogin = autoLogin,
            Continue = @continue,
            DisplayName = displayName,
            UserName = userName,
            Email = email,
            FirstName = firstName,
            LastName = lastName,
            Password = password
        };
        redisUser.Store(yeniKayit);
        return yeniKayit.Continue;
    }
}

我创建了这样的代码,它保存到redis但是这次auth不起作用?

我如何使用ServiceStack注册功能插件与Redis

如果你启用RegistrationFeature并使用我在这个答案中建议的Redis设置(如下所示),那么注册将像正常的ServiceStack身份验证一样工作。

private IRedisClientsManager redisClientsManager;
public override void Configure(Funq.Container container)
{
    // Configure ServiceStack to connect to Redis
    // Replace with your connection details
    redisClientsManager = new PooledRedisClientManager("127.0.0.1:6379");
    container.Register<IRedisClientsManager>(c => redisClientsManager);
    container.Register<ICacheClient>(c => c.Resolve<IRedisClientsManager>().GetCacheClient()).ReusedWithin(Funq.ReuseScope.None);
    // Setup the authorisation feature
    Plugins.Add(new AuthFeature(()=> 
        new AuthUserSession(),
        new IAuthProvider[]{ new BasicAuthProvider() }
    ));
    // Use a RedisAuthRepository
    var userRepo = new RedisAuthRepository(redisClientsManager);
    container.Register<IUserAuthRepository>(userRepo);
    // Enable the RegistrationFeature
    Plugins.Add(new RegistrationFeature());
}

所以如果你发布一个有效的注册请求到/register,那么用户将在Redis存储库中创建。

{
    "UserName": "john.smith",
    "Password": "test",
    "Email": "john.smith@domain.com",
    "FirstName": "John",
    "LastName": "Smith",
    "DisplayName": "John Smith"
}