如何在Redis中使用ServiceStack RegistrationFeature插件?

时间:2014-07-17 11:11:19

标签: c# redis servicestack authentication

我正在添加

Plugins.Add(new RegistrationFeature());

现在我已经获得了Register服务的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);

和Redis页面:

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不起作用?

1 个答案:

答案 0 :(得分:1)

如果您启用RegistrationFeature并使用the Redis setup I suggested in this answer(完整性如下所示),则注册将像普通的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"
}