public async Task<ActionResult> ConfirmEmail(int userId, string code) { if (userId == 0 || code == null) { TempData.Add("confirmEmail", "No se ha podido confirmar el email"); return View("Login"); } IdentityResult result; try { var provider = new MachineKeyProtectionProvider(); UserManager<User, int> um = new UserManager<User, int>(ur); um.UserTokenProvider = new DataProtectorTokenProvider<User, int>(provider.Create("EmailConfirmation")); result = await um.ConfirmEmailAsync(userId, code); } catch (ArgumentNullException) { // ConfirmEmailAsync throws when the userId is not found. TempData.Add("confirmEmail", "Usuario no encontrado"); return View("Login"); } catch(ArgumentException) { TempData.Add("confirmEmail", "El email ya ha sido validado anteriormente"); return View("Login"); } if (result.Succeeded) { TempData.Add("confirmEmail", "Se ha confirmado su email correctamente"); return View("Login"); } TempData.Add("confirmEmail", result); return View("Login"); }
private void sendConfirmationEmail(User user) { var provider = new MachineKeyProtectionProvider(); UserManager<User, int> um = new UserManager<User, int>(ur); um.UserTokenProvider = new DataProtectorTokenProvider<User, int>(provider.Create("EmailConfirmation")); um.EmailService = new EmailService(); TempData.Add("confirmEmail", "Le hemos enviado un correo electrónico para confirmar su cuenta, comprube la carpeta spam"); if (Url != null) { var code = um.GenerateEmailConfirmationToken(user.U_id); var callbackUrl = Url.Action( "ConfirmEmail", "Home", new { userId = user.Id, code = code }, protocol: "http"); um.SendEmail(user.Id, "Confirma tu correo", "Por favor confirme su correo haciendo click en este <a href=\"" + callbackUrl + "\">link</a>"); } }