public async Task <Customer> Register(CustomerToRegister customerToRegister)
        {
            var newCustomer = new Customer();

            newCustomer.Email = customerToRegister.Email;
            // encrypt password
            byte[] salted = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salted);
            }
            string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                       password: customerToRegister.Password,
                                                       salt: salted,
                                                       prf: KeyDerivationPrf.HMACSHA1,
                                                       iterationCount: 10000,
                                                       numBytesRequested: 256 / 8));

            // store password hashed for new customer
            newCustomer.PasswordHashed = hashed;
            newCustomer.PasswordSalt   = salted;
            newCustomer.DateCreated    = DateTime.Now;
            // add new customer to database
            await _context.Customer.AddAsync(newCustomer);

            await _context.SaveChangesAsync();

            return(newCustomer);
        }
 public async Task<IActionResult> Register(CustomerToRegister customerToRegister)
 {
     // lowercase email
     customerToRegister.Email.ToLower();
     // check if email already in database 
     // if (await _dataContext.Customer.AnyAsync(xxx => xxx.Email == customerToRegister.Email))
     if (await _repo.UserExists(customerToRegister.Email))
         return BadRequest(new { message = "This Email is already registered" });
     // Register user if email is not existed 
     else
     {
        
         var newCustomer = _repo.Register(customerToRegister);
         return StatusCode(201);
     }
 }
Exemplo n.º 3
0
        public async Task Register_Fail_Email_AlreadyUsed_ResponseBadRequest(string email, string password)
        {
            Mock <IAuthRepository> mockIAuthRepository = new Mock <IAuthRepository>();
            Mock <IOptions <ApplicationSettings> > mockIOptions_ApllicationSettings = new Mock <IOptions <ApplicationSettings> >();
            var customerToRegister = new CustomerToRegister()
            {
                Email    = email,
                Password = password
            };

            // Set up default return from repo
            mockIAuthRepository.Setup(repo => repo.UserExists(It.IsAny <String>()))
            .Returns(Task.FromResult(true)); // true -> email has already existed in system -> user cannot register
            var authController = new AuthController(mockIAuthRepository.Object, mockIOptions_ApllicationSettings.Object);
            // Get a status code reponse back from call register method in auth controller
            var statusCode = await authController.Register(customerToRegister);

            // Expect Bad Request instance
            Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.BadRequestObjectResult>(statusCode);
        }
Exemplo n.º 4
0
        public async Task Register_Successful_ResponseStatusCode201(string email, string password)
        {
            Mock <IAuthRepository> mockIAuthRepository = new Mock <IAuthRepository>();
            Mock <IOptions <ApplicationSettings> > mockIOptions_ApllicationSettings = new Mock <IOptions <ApplicationSettings> >();
            var customerToRegister = new CustomerToRegister()
            {
                Email    = email,
                Password = password
            };

            // Set up default return from repo
            mockIAuthRepository.Setup(repo => repo.UserExists(It.IsAny <String>()))
            .Returns(Task.FromResult(false)); // false -> email not existed in system -> user able to register
            mockIAuthRepository.Setup(repo => repo.Register(It.IsAny <CustomerToRegister>()))
            .ReturnsAsync(new Customer());    // return new user when register successfully
            var authController = new AuthController(mockIAuthRepository.Object, mockIOptions_ApllicationSettings.Object);
            // Get a status code reponse back from call register method in auth controller
            var statusCode = await authController.Register(customerToRegister);

            // Expect 201
            Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.StatusCodeResult>(statusCode);
        }
Exemplo n.º 5
0
 public async Task <IActionResult> Register(CustomerToRegister customerToRegister)
 {
     // lowercase email
     customerToRegister.Email.ToLower();
     // check if email already in database
     // if (await _dataContext.Customer.AnyAsync(xxx => xxx.Email == customerToRegister.Email))
     if (await _repo.UserExists(customerToRegister.Email))
     {
         return(BadRequest(new { message = "This Email is already registered" }));
     }
     // Register user if email is not existed
     else
     {
         // create new customer
         // var newCustomer = new Customer();
         // newCustomer.Email = customerToRegister.Email;
         // // encrypt password
         // byte[] salted = new byte[128 / 8];
         // using (var rng = RandomNumberGenerator.Create())
         // {
         //     rng.GetBytes(salted);
         // }
         // string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
         //       password: customerToRegister.Password,
         //       salt: salted,
         //       prf: KeyDerivationPrf.HMACSHA1,
         //       iterationCount: 10000,
         //       numBytesRequested: 256 / 8));
         // // store password hashed for new customer
         // newCustomer.PasswordHashed = hashed;
         // newCustomer.PasswordSalt = salted;
         // newCustomer.DateCreated = DateTime.Now;
         // // add new customer to database
         // await _dataContext.Customer.AddAsync(newCustomer);
         // await _dataContext.SaveChangesAsync();
         var newCustomer = _repo.Register(customerToRegister);
         return(StatusCode(201));
     }
 }