public ActionResult SignUp(AccountSignUp account) { if (ModelState.IsValid) { ToDoSignUp TDSU = new ToDoSignUp(); TDSU.Registration(account); return(RedirectToAction("Index")); } return(View(account)); }
public void Registration(AccountSignUp ASU) { using (TDList list = new TDList()) { Account account = new Account { Login = ASU.Login, Password = CryptoProvider.GetMD5Hash(ASU.Password) }; list.Accounts.Add(account); list.SaveChanges(); } }
public async void account_service_creates_new_user_correctly() { string dbName = Guid.NewGuid().ToString(); DbContextOptions <PlayItContext> options = new DbContextOptionsBuilder <PlayItContext>() .UseInMemoryDatabase(databaseName: dbName).Options; var unitOfWork = new UnitOfWork <PlayItContext>(new PlayItContext(options)); var dbLogger = new DbExceptionLogger(unitOfWork); IAccountService aService = new AccountService(new PasswordService(new SHA512HashingService(), 8, 32), unitOfWork, new TokenService(unitOfWork, dbLogger), new MailService(), dbLogger); var accountSignUp = new AccountSignUp("*****@*****.**", "TestUserName", "123123Asda!as", ""); var accountSignUpFail = new AccountSignUp("", "", "", ""); //Success var account = await aService.CreateAccount(accountSignUp); Assert.True(account.Entity != null); //AccountSignUp Null await Assert.ThrowsAsync <NullReferenceException>(() => null); //userName parameter null or empty await Assert.ThrowsAsync <ArgumentNullException>(async() => await aService.CreateAccount(accountSignUpFail)); //Email parameter null or empty accountSignUpFail.UserName = "******"; await Assert.ThrowsAsync <ArgumentNullException>(async() => await aService.CreateAccount(accountSignUpFail)); //Password parameter null or empty accountSignUpFail.Email = "*****@*****.**"; await Assert.ThrowsAsync <ArgumentNullException>(async() => await aService.CreateAccount(accountSignUpFail)); accountSignUpFail.Password = "******"; Assert.True(await aService.CreateAccount(accountSignUpFail) != null); }
public async void account_gets_verified_correctly() { string dbName = Guid.NewGuid().ToString(); DbContextOptions <PlayItContext> options = new DbContextOptionsBuilder <PlayItContext>() .UseInMemoryDatabase(databaseName: dbName).Options; var unitOfWork = new UnitOfWork <PlayItContext>(new PlayItContext(options)); var dbLogger = new DbExceptionLogger(unitOfWork); IAccountService aService = new AccountService(new PasswordService(new SHA512HashingService(), 8, 32), unitOfWork, new TokenService(unitOfWork, dbLogger), new MailService(), dbLogger); //Create an account var accountSignUp = new AccountSignUp("*****@*****.**", "TestUserName", "123123Asda!as", ""); var account = await aService.CreateAccount(accountSignUp); //Token verified success Assert.True(await aService.VerifyAccount(account.Entity.Token.First().TokenId) != null); Assert.True((await aService.VerifyAccount(account.Entity.Token.First().TokenId)).Verified); //Token does not exists await Assert.ThrowsAsync <NullReferenceException>(async() => await aService.VerifyAccount("a")); }
public async ValueTask <EntityEntry <Models.Entities.Account> > CreateAccount(AccountSignUp accountSignUp) { if (accountSignUp == null) { throw new ArgumentNullException(nameof(accountSignUp)); } if (string.IsNullOrEmpty(accountSignUp.UserName)) { throw new ArgumentNullException(accountSignUp.UserName); } if (string.IsNullOrEmpty(accountSignUp.Email)) { throw new ArgumentNullException(accountSignUp.Email); } if (string.IsNullOrEmpty(accountSignUp.Password)) { throw new ArgumentNullException(accountSignUp.Password); } //Create a new password for the customer byte[] password = new byte[64], salt = new byte[32]; EntityEntry <Models.Entities.Account> account; //The avatar path string avatarPath = SaveAvatar(accountSignUp.Avatar, accountSignUp.UserName); try { //Create account password password = _passwordService.CreatePassword(accountSignUp.Password, out salt); //Create customer in database var accountRepo = _unitOfWork.GetRepository <Models.Entities.Account>(); account = await accountRepo.InsertAsync(new Models.Entities.Account { Created = DateTime.Now, Email = accountSignUp.Email, Password = password, Salt = salt, Verified = false, UserName = accountSignUp.UserName, AvatarFilePath = avatarPath }); await _unitOfWork.SaveChangesAsync(); //Check if created in db if (account != null) { //Create Token var token = await _tokenService.CreateToken(account.Entity.AccountId, 1); //Send mail to user with verification token _mailService.SendMail("444.dk - Godkendelse af bruger oprettelse", "<!DOCTYPE html><html> <head> <style> .logo { text-align: center; } .main { text-align: center; } .footer { text-align: center; } .btn { display: inline-block; font-weight: 400; color: #212529; text-align: center; vertical-align: middle; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background-color: transparent; border: 1px solid transparent; padding: .375rem .75rem; font-size: 1rem; line-height: 1.5; border-radius: .25rem; transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out; } .btn-success { color: #fff; background-color: #28a745; border-color: #28a745; } .footer span { color: #777; } </style> </head> <body> <div class=\"container\"> <div class=\"logo\"> <img src=\"https://image.444.dk/logo/logo-without-banner.png\" alt=\"444.dk Logo\" width=\"180\" height=\"140\"> </div> <br> <div class=\"main\"> <span>Tryk på godkend herunder for at verificere din email</span><br> <a class=\"btn btn-success\" href=\"https://444.dk?token=" + token.Entity.TokenId + "\">Godkend</a> </div> <br> <div class=\"footer\"> <span>Har du ikke oprettet dig på 444.dk?</span><br> <span>Venligst ignorer denne mail.</span> </div> </div> </body></html>", account.Entity.Email); } } catch (ArgumentNullException e) { await _logger.LogAsync(e.Message); throw; } catch (DbException e) { await _logger.LogAsync(e.Message); throw; } catch (Exception e) { await _logger.LogAsync(e.Message); throw; } return(account); }
public async Task <IActionResult> CreateAccount(AccountSignUp accountSignUp) { //Check parameters if (accountSignUp == null) { return(BadRequest("Udfyld alle felter")); } if (string.IsNullOrEmpty(accountSignUp.Email)) { return(BadRequest("Email kan ikke være tomt")); } if (string.IsNullOrEmpty(accountSignUp.Password)) { return(BadRequest("Adgangskode kan ikke være tomt")); } if (string.IsNullOrEmpty(accountSignUp.UserName)) { return(BadRequest("Brugernavn kan ikke være tomt")); } //Validate Email if (!Regex.IsMatch(accountSignUp.Email, "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") ) { return(BadRequest("Email er ikke valid")); } //Validate Password if (!Regex.IsMatch(accountSignUp.Password, _passwordService.PasswordPattern)) { return(BadRequest( $"Adgangskoden var ikke valid, adgangskoden skal minimum indeholde 1 stortbogstav, 1 bogstav, 1 specialtegn, 1 tal og være {_passwordService.MinLength}-{_passwordService.MaxLength} tegn langt")); } //Check if account userName already exists if (await _accountService.AccountExists(accountSignUp.Email)) { return(BadRequest("En bruger med denne brugernavn eksistere allerede")); } //Check if account email already exists if (await _accountService.EmailExists(accountSignUp.Email)) { return(BadRequest("En bruger med denne email eksistere allerede")); } try { //Create account if (await _accountService.CreateAccount(accountSignUp) != null) { //If successfully send ok created return(Ok(new Response("Oprettet bruger!"))); } } catch (Exception) { return(BadRequest( "Der skete en fejl ved oprettelse af brugere, kontakt venligst [email protected] hvis dette fortsætter")); } return(BadRequest( "Der skete en fejl ved oprettelse af brugere, kontakt venligst [email protected] hvis dette fortsætter")); }