您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从Redis中的值填充UserAuth?

如何从Redis中的值填充UserAuth?

是的,您可以针对Redis数据源进行身份验证。您可以使用内置RedisAuthRepository代替InMemoryAuthRepository,或者如果您要使用现有的Redis数据集而不是内置IAuthRepository模式,则我提供了一个解决方案,您可以扩展BasicAuthProvider。第一种方法最简单:

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

        // You can then register users as required using the RegistrationFeature
    }

您可以通过创建扩展现有BasicAuthProvider自定义身份验证提供程序来实现

对于此代码,还应该确保您熟悉ServiceStack.Redis客户端

MyRedisBasicAuthProvider将扩展现有的BasicAuthProvider,而不是IUserAuthRepository像示例代码中所提供的那样从中执行凭据查找,而是建立Redis连接并将用户名与Redis中的条目匹配。

代码已完全注释,但是如果您希望进一步解释,请告诉我。

    public class MyRedisBasicAuthProvider : BasicAuthProvider
    {
        // The key at which we will store the user profile. i.e user:john.smith or user:homer.simpson
        // Replace this key with your format as required
        public const string UserKeyFormat = "user:{0}";

        MyUser CurrentUser;

        // Gets an instance of a redis client
        static IRedisClient GetRedisClient()
        {
            // Get the RedisClientsManager from the Container
            var redisClientManager = HostContext.TryResolve<IRedisClientsManager>();
            if(redisClientManager == null)
                throw new Exception("Redis is not configured");

            // Return a client
            return redisClientManager.GetClient();
        }

        // This method is used to verify the credentials provided
        public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            // Get a Redis client connection
            using(var redisClient = GetRedisClient())
            {
                // Get a typed Redis Client
                var userClient = redisClient.As<MyUser>();

                // Try to find a matching user in Redis
                CurrentUser = userClient.GetValue(string.Format(UserKeyFormat, userName));

                // Check the user exists & their password is correct (You should use a hashed password here)
                return CurrentUser != null && password == CurrentUser.Password;
            }
        }

        // This method is used to populate the session details from the user profile and other source data as required
        public override IHttpResult OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
        {
            // Populate the session with the details of the current user
            session.PopulateWith<IAuthSession, MyUser>(CurrentUser);

            // Save the session
            authService.SaveSession(session);

            return null;
        }

        public static void AddUserToRedis(MyUser user)
        {
            using(var redisClient = GetRedisClient())
            {
                // Get a typed Redis Client
                var userClient = redisClient.As<MyUser>();

                // Add the user to Redis
                userClient.SetEntry(string.Format(UserKeyFormat, user.Username), user);
            }
        }
    }

在上面的代码中,我使用了一个MyUser来表示我存储在Redis中的用户个人资料,您当然可以自定义此类以匹配您的用户个人资料要求。因此,这是基本的用户配置文件类:

    public class MyUser
    {
        public string Username { get; set; }
        public string Password { get; set; } // Replace with a hashed password
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

您将需要配置ServiceStack以使用Redis,并告诉它使用您的自定义身份验证提供程序。为此,您可以在的Configure方法添加以下内容AppHost

    public override void Configure(Funq.Container container)
    {
        // Configure ServiceStack to connect to Redis
        // Replace with your connection details
        container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("127.0.0.1:6379"));
        container.Register<ICacheClient>(c => c.Resolve<IRedisClientsManager>().GetCacheClient()).ReusedWithin(Funq.ReuseScope.None);

        // Add your custom credentials provider
        Plugins.Add(new AuthFeature(() => new AuthUserSession(),
            new IAuthProvider[] {
                new MyRedisBasicAuthProvider()
            }
        ));

        // Add some test users. (If you have an existing Redis user source, you won't need to add test users.)
        MyRedisBasicAuthProvider.AddUserToRedis(new MyUser {
            Username = "john.smith",
            Password = "test",
            Email = "john.smith@email.com",
            FirstName = "John",
            LastName = "Smith",
        });

        MyRedisBasicAuthProvider.AddUserToRedis(new MyUser {
            Username = "homer.simpson",

            Password = "donuts",
            Email = "homer.simpsons@springfield.com",
            FirstName = "Homer",
            LastName = "Simpson",
        });

        // Your other configuration settings ...
    }

在示例中,我没有使用哈希密码来使示例简单明了,但这很简单。在字段中添加一个字段public string Salt { get; set; }MyUser而不是将普通密码MyUser存储为密码和salt的哈希值,即hashedPassword = HashAlgorithm(password + salt)。您已经有了代码

string hash, salt;
new SaltedHash().GetHashAndSaltString("password", out hash, out salt);

因此,当使用[Authenticate]属性保护服务安全时,此解决方案现在将使用Redis数据源对用户进行身份验证。与标准基本提供程序一样,凭据在标准/auth/basic路由上进行身份验证。

如果你想使用的表单提交一个证书提供商,而不是基本身份验证,您可以简单替换的单词BasicCredentials在上面的代码

我希望这有帮助。

其他 2022/1/1 18:16:36 有531人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶