public async Task <JsonResult> ResetPasswordGenerateToken(string email) { AppUser user = await userManager.FindByEmailAsync(email); if (user != null) { string token = await userManager.GeneratePasswordResetTokenAsync(user); string link = $@"{this.Request.Scheme}://{this.Request.Host}/#/resetPassword?userId={user.Id}&token={token}"; MailMessage mes = new MailMessage("*****@*****.**", "*****@*****.**", "aktywuj", link); MyEmailClient myEmailClient = new MyEmailClient(); await myEmailClient.SendEmail(mes); return(Json(new { Success = true })); } return(Json(new { Success = false, Errors = new List <string> { $"Użytkownik o adresie e-mail: {email} nie istnieje." } })); }
public void SendEmail_NoEmailService_Throws() { string to = "destination", header = "Great opportunity", body = "I am the Nigerian finance minister"; MyEmailClient ec = new MyEmailClient(); Assert.Throws <NullReferenceException>(() => ec.TryToSendEmail(to, header, body)); }
public async Task <JsonResult> Register([FromBody] CreateModel model) { if (ModelState.IsValid && model.Password == model.PasswordRepeated) { AppUser newUser = new AppUser { Email = model.Email, UserName = model.UserName }; IdentityResult result = await userManager.CreateAsync(newUser, model.Password); if (result.Succeeded) { string confirmationToken = userManager .GenerateEmailConfirmationTokenAsync(newUser).Result; string link = $@"{this.Request.Scheme}://{this.Request.Host}/#/confirmEmail?userId={newUser.Id}&token={confirmationToken}"; MailMessage mes = new MailMessage("*****@*****.**", "*****@*****.**", "aktywuj", link); MyEmailClient myEmailClient = new MyEmailClient(); await myEmailClient.SendEmail(mes); return(Json(new { Success = true, Message = "Grtulacje!" })); } // identityResult == false List <string> errors = new List <string>(); foreach (var err in result.Errors) { errors.Add(err.Description); } return(Json(new { Success = false, Errors = errors })); } return(Json(new { Success = false, Errors = new List <string> { "Wprowadzone dane nie spełniają wymagań aplikacji. Popraw je i spróbuj jeszcze raz." } })); }
public void SendEmail_InvalidValues_Throws() { string to = "destination", header = "Great opportunity", body = "I am the Nigerian finance minister"; MyEmailClient ec = new MyEmailClient(); //ec.EmailService = new FakeEmailService(); var mock = new Mock <IEmailService>(); Assert.Throws <Exception>(() => ec.TryToSendEmail( null, header, body)); Assert.Throws <Exception>(() => ec.TryToSendEmail( to, null, body)); Assert.Throws <Exception>(() => ec.TryToSendEmail( to, header, null)); /*Assert.Throws<Exception>(() => ec.TryToSendEmail( * to, null, null)); <- onödig */ // TODO: test empty strings as well }
public void SendEmail_CorrectValues_Success() { string to = "destination", header = "Great opportunity", body = "I am the Nigerian finance minister"; MyEmailClient ec = new MyEmailClient(); ec.Name = "Antonio"; ec.Email = "*****@*****.**"; var mock = new Mock <IEmailService>(); ec.EmailService = mock.Object; //FakeEmailService fake = new FakeEmailService(); //ec.EmailService = fake; ec.TryToSendEmail(to, header, body); string from = ec.Name + " <" + ec.Email + ">"; mock.Verify(x => x.SendEmail(to, from, header, body), Times.Once()); // Assert.Equal(1, fake.HowManyTimesHasSendEmailBeenCalled); }
public async Task <JsonResult> UpdateUser([FromBody] UpdateUserModel model) { AppUser user = await userManager.GetUserAsync(HttpContext.User); if (user != null) { if (userManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, model.Password) == PasswordVerificationResult.Success) { //jesli nowa nazwa jest inna - zmienia IdentityResult updateUserNameResult = null; if (user.UserName != model.UserName) { updateUserNameResult = await userManager.SetUserNameAsync(user, model.UserName); if (!updateUserNameResult.Succeeded) { List <string> errors = new List <string>(); foreach (var err in updateUserNameResult.Errors) { errors.Add(err.Description); } return(Json(new { Success = false, Errors = errors })); } await signInManager.RefreshSignInAsync(user); } // jesli nowy email jest inny - zmienia if (model.Email != user.Email) { string token = await userManager.GenerateChangeEmailTokenAsync(user, model.Email); string link = Url.Action("ChangeEmail", "User", new { userId = user.Id, token, email = model.Email }, protocol: HttpContext.Request.Scheme); MailMessage mes = new MailMessage("*****@*****.**", "*****@*****.**", "aktywuj", link); MyEmailClient myEmailClient = new MyEmailClient(); await myEmailClient.SendEmail(mes); } if (updateUserNameResult != null && model.Email != user.Email) { return(Json(new { Success = true, UserNameChanged = true, user.UserName, EmailChanged = true })); } else if (updateUserNameResult != null && model.Email == user.Email) { return(Json(new { Success = true, UserNameChanged = true, user.UserName, EmailChanged = false })); } else if (updateUserNameResult == null && model.Email != user.Email) { return(Json(new { Success = true, UserNameChanged = false, EmailChanged = true })); } } return(Json(new { Success = false, Errors = new List <string> { "Wprowadzone hasło jest nieprawidłowe!" } })); } return(Json(new { Success = false, Errors = new List <string> { "Odśwież stronę i zaloguj się ponownie!" } })); }