Exemplo n.º 1
0
        public TUser CreateOrUpdate(TUser user, string userId = null)
        {
            var baseKey = RedisKey.Build(Keys.AspNetUsers);

            //insert
            if (string.IsNullOrEmpty(userId))
            {
                user.Id = db.IncrementValue(baseKey).ToString();
                var uniqueId = string.Format("{0}:{1}", user.UserName, user.Id);
                db.Add <TUser>(RedisKey.Build(Keys.AspNetUsers, id: uniqueId), user);
            }
            else
            {
                //update
                user.Id = userId;
                var NewUser = (TUser)Activator.CreateInstance(typeof(TUser));
                NewUser.Id    = user.Id;
                NewUser.Email = user.Email;
                NewUser.NormalizedUserName = user.NormalizedUserName;
                NewUser.PasswordHash       = user.PasswordHash;
                NewUser.SecurityStamp      = user.SecurityStamp;
                NewUser.UserName           = user.UserName;

                //logins

                for (int i = 0; i < user.Logins.Count; i++)
                {
                    UserLoginInfo info = new UserLoginInfo(user.Logins[i].LoginProvider, user.Logins[i].ProviderKey, user.Logins[i].ProviderDisplayName);
                    NewUser.Logins.Add(info);
                }
                //roles
                for (int i = 0; i < user.Roles.Count; i++)
                {
                    NewUser.Roles.Add((user.Roles[i]));
                }
                //claim
                for (int i = 0; i < user.Claims.Count; i++)
                {
                    NewUser.Claims.Add((user.Claims[i]));
                }

                var uniqueId = string.Format("{0}:{1}", NewUser.UserName, NewUser.Id);
                db.Set <TUser>(RedisKey.Build(Keys.AspNetUsers, id: uniqueId), NewUser);
            }

            //Add UserLogin Key-Value
            foreach (var login in user.Logins)
            {
                db.Set(RedisKey.Build(Keys.UserLoginInfo, entity: login), user.Id);
            }
            return(user);
        }
Exemplo n.º 2
0
        public Task <TUser> FindByNameAsync(string userName)
        {
            this.ThrowIfDisposed();
            var uniqueKey = string.Format("{0}:{1}", userName, "*");
            var keys      = db.SearchKeys(RedisKey.Build(Keys.AspNetUsers, id: uniqueKey));

            if (keys.Any())
            {
                uniqueKey = keys[0];
            }
            var user = db.Get <TUser>(uniqueKey);

            return(Task.FromResult(user));
        }
Exemplo n.º 3
0
        public Task DeleteAsync(TUser user)
        {
            CheckDisposed(user);
            var baseKey   = RedisKey.Build(Keys.AspNetUsers);
            var uniqueKey = string.Format("{0}:{1}", user.UserName, user.Id);

            db.Remove(RedisKey.Build(Keys.AspNetUsers, id: uniqueKey));
            //Add UserLogin Key-Value
            foreach (var login in user.Logins)
            {
                var key = RedisKey.Build(Keys.UserLoginInfo, entity: login);
                if (db.ContainsKey(key))
                {
                    db.Remove(key);
                }
            }
            return(Task.FromResult(true));
        }
Exemplo n.º 4
0
        public Task <TUser> FindAsync(UserLoginInfo login)
        {
            TUser user      = null;
            var   loginKeys = db.SearchKeys(RedisKey.Build(Keys.UserLoginInfo, entity: login));

            foreach (var loginKey in loginKeys)
            {
                var id = db.Get <string>(loginKey);
                if (id != string.Empty)
                {
                    user = FindByIdAsync(id).Result;
                    if (user != null)
                    {
                        break;
                    }
                }
            }
            return(Task.FromResult(user));
        }
Exemplo n.º 5
0
        public TUser CreateOrUpdate(TUser user, string userId = null)
        {
            var baseKey = RedisKey.Build(Keys.AspNetUsers);

            if (string.IsNullOrEmpty(userId))
            {
                user.Id = db.IncrementValue(baseKey).ToString();
            }
            else
            {
                user.Id = userId;
            }
            var uniqueId = string.Format("{0}:{1}", user.UserName, user.Id);

            db.Add <TUser>(RedisKey.Build(Keys.AspNetUsers, id: uniqueId), user);

            //Add UserLogin Key-Value
            foreach (var login in user.Logins)
            {
                db.Add(RedisKey.Build(Keys.UserLoginInfo, entity: login), user.Id);
            }
            return(user);
        }