Ejemplo n.º 1
0
        public async Task <ActionResult <UserDto> > Register(RegisterDTOs registerDto)
        {
            if (await UserExists(registerDto.Username))
            {
                return(BadRequest("Username is taken"));
            }

            //using var hmac = new HMACSHA512();
            // var user = new AppUser{
            //     UserName = registerDto.Username.ToLower(),
            //     PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password)),
            //     PasswordSalt = hmac.Key
            // };
            var user = _mapper.Map <AppUser>(registerDto);

            user.UserName = registerDto.Username.ToLower();
            //user.PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password));
            //user.PasswordSalt = hmac.Key;

            // _context.Users.Add(user);
            // await _context.SaveChangesAsync();
            var result = await _userManager.CreateAsync(user, registerDto.Password);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors));
            }

            var roleResult = await _userManager.AddToRoleAsync(user, "Member");

            if (!roleResult.Succeeded)
            {
                return(BadRequest(result.Errors));
            }

            //return user;
            return(new UserDto {
                Username = user.UserName,
                Token = await _tockenService.CreateTocken(user),
                KnownAs = user.KnownAs,
                Gender = user.Gender
            });
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <UserDto> > Register(RegisterDto registerDto)
        {
            if (await userExists(registerDto.Username))
            {
                return(BadRequest("UserName is taken"));
            }
            using var hmac = new HMACSHA512();
            var user = new AppUser
            {
                UserName     = registerDto.Username.ToLower(),
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password)),
                PasswordSalt = hmac.Key
            };

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(new UserDto
            {
                Username = user.UserName,
                Token = _tokenService.CreateTocken(user)
            });
        }