Пример #1
0
        /// <summary>
        /// 单条添加用户角色信息
        /// </summary>
        /// <param name="usersInfo"></param>
        /// <returns></returns>
        public int Add(UsersInfo usersInfo)
        {
            //实例化用户表
            Users users = new Users();

            users.UserName = usersInfo.UserName;
            users.Password = usersInfo.Password;
            var result = UsersDB.Insert(users);

            //如果result为true
            if (result)
            {
                SqlSugarClient sqlSugarClient = Educationcontext.GetClient();
                var            db             = sqlSugarClient.SqlQueryable <Users>("select Id from Users order by Id  ").Max(s => s.Id);
                //实例化用户角色表
                UserandRole userandRole = new UserandRole();
                userandRole.UsersId = db;
                var num = usersInfo.RoleName.Substring(0, usersInfo.RoleName.LastIndexOf(',')).Split(',');

                var usersdb = 0;
                foreach (var item in num)
                {
                    userandRole.RolesId = int.Parse(item);
                    usersdb            += sqlSugarClient.Insertable <UserandRole>(userandRole).ExecuteCommand();
                }
                if (usersdb == num.Length)
                {
                    return(1);
                }
            }
            return(0);
        }
Пример #2
0
        public async Task <AuthUserDto> RegisterUser(AuthUserDto user)
        {
            var userrep = await _context.AuthUser.FirstOrDefaultAsync(x => x.UserName == user.UserName);

            string[] roleSplit = user.RoleName.Split(new char[] { ',' });

            foreach (string roleVal in roleSplit)
            {
                if (_context.UserRole != null)
                {
                    //cannit use string.compare
                    var role = await _context.UserRole.FirstOrDefaultAsync(
                        x => !string.IsNullOrWhiteSpace(roleVal) &&
                        !string.IsNullOrWhiteSpace(x.RoleName) &&
                        roleVal.ToLower() == x.RoleName.ToLower());

                    if (role == null)
                    {
                        role          = new Role();
                        role.RoleName = user.RoleName;
                        await _context.UserRole.AddAsync(role);

                        await _context.SaveChangesAsync();
                    }
                }
            }

            if (userrep == null)
            {
                userrep = new AuthUser();
            }

            userrep.UserName  = user.UserName;
            userrep.FirstName = user.FirstName;
            userrep.LastName  = user.LastName;
            userrep.Email     = user.Email;
            userrep.Address   = user.Address;
            userrep.UserandRole.Clear();


            userrep.UserandRole = new List <UserandRole>();

            foreach (string roleVal in roleSplit)
            {
                UserandRole userRole = new UserandRole();

                /*var role = await _context.UserRole.FirstOrDefaultAsync(
                 *  x => String.Equals(x.RoleName, roleVal,
                 *  StringComparison.OrdinalIgnoreCase));*/
                var role = await _context.UserRole.FirstOrDefaultAsync(
                    x => !string.IsNullOrWhiteSpace(roleVal) &&
                    !string.IsNullOrWhiteSpace(x.RoleName) &&
                    roleVal.ToLower() == x.RoleName.ToLower());

                userRole.UserRole   = role;
                userRole.RoleId     = role.RoleId;
                userRole.AuthUser   = userrep;
                userRole.AuthUserId = userrep.AuthUserId;
                userrep.UserandRole.Add(userRole);
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(user.Password, out passwordHash, out passwordSalt);
            userrep.PasswordHash = passwordHash;
            userrep.PasswordSalt = passwordSalt;
            await _context.AuthUser.AddAsync(userrep);

            await _context.SaveChangesAsync();

            return(user);
        }