Exemplo n.º 1
0
        /// <summary>
        /// 将用户添加到组
        /// </summary>
        /// <param name="userGroupId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task AddUserToGroupAsync(long userGroupId, long userId)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var group = await ctx.UserGroups.SingleOrDefaultAsync(g => g.Id == userGroupId);

                if (group == null)
                {
                    throw new AggregateException("userGroupId=" + userGroupId + "不存在");
                }
                var user = await ctx.Users.SingleOrDefaultAsync(u => u.Id == userId);

                if (user == null)
                {
                    throw new AggregateException("userId=" + userId + "不存在");
                }
                group.Users.Add(user);
                user.Groups.Add(group);
                await ctx.SaveChangesAsync();
            }
        }
Exemplo n.º 2
0
        public async Task <long> AddNewAsync(string phoneNum, string nickName, string password)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                if (await ctx.Users.AnyAsync(u => u.PhoneNum == phoneNum))
                {
                    throw new ApplicationException("手机号:" + phoneNum + "已经存在");
                }
                UserEntity userEntity = new UserEntity();
                userEntity.NickName = nickName;
                userEntity.PhoneNum = phoneNum;
                string salt = new Random().Next(10000, 99999).ToString();
                string hash = MD5Helper.CalcMD5(password + salt);
                userEntity.PasswordSalt = salt;
                userEntity.PasswordHash = hash;
                ctx.Users.Add(userEntity);
                await ctx.SaveChangesAsync();

                return(userEntity.Id);
            }
        }