Example #1
0
      public async Task <ActionResult <UserDto> > Register(RegisterDTo registerDto)
      {
          if (await _userManager.Users.AnyAsync(x => x.Email == registerDto.Email))
          {
              return(BadRequest("Email Taken"));
          }
          if (await _userManager.Users.AnyAsync(x => x.UserName == registerDto.Username))
          {
              return(BadRequest("Username Taken"));
          }

          var user = new AppUser
          {
              DisplayName = registerDto.DisplayName,
              Email       = registerDto.Email,
              UserName    = registerDto.Username
          };

          var result = await _userManager.CreateAsync(user, registerDto.Password);

          if (result.Succeeded)
          {
              return(CreateUserObject(user));
          }

          return(BadRequest("Problem registering user"));
      }
Example #2
0
        public async Task <ActionResult <UserDTo> > Register(RegisterDTo registerDTo)
        {
            if (await UserExists(registerDTo.Username))
            {
                return(BadRequest("Username is taken"));
            }

            var user = _mapper.Map <AppUser>(registerDTo);

            user.UserName = registerDTo.Username.ToLower();

            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(new UserDTo
            {
                Username = user.UserName,
                Token = await _tokenService.CreateToken(user),
                KnownAs = user.KnownAs,
                Gender = user.Gender
            });
        }
Example #3
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,
                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.CreateToken(user)
            });
        }
Example #4
0
        public async Task <ActionResult <UserToken> > Register([FromBody] RegisterDTo model)
        {
            var user = new UserEntity
            {
                UserName = model.UserName,
                Email    = model.Email,
            };

            var result = await _userManager.CreateAsync(user, model.passWord);

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

            return(GenerateJwtToken(model.Email, user));
        }