public async Task <ActionResult> Registrarse(RegistrarseViewModel model) { model.Estado = true; if (ModelState.IsValid) { Paciente user = new Paciente { UserName = model.Email, Email = model.Email, Nombre = model.Nombre, Apellido = model.Apellido, Estado = model.Estado }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await this.UserManager.AddToRoleAsync(user.Id, "Paciente"); var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmarEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirmar su cuenta", "Por favor para confirmar su cuenta haga click en el siguiente enlace: <a href=\"" + callbackUrl + "\">link</a>"); //ViewBag.Link = callbackUrl; return(View("DisplayEmail")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Registrarse(RegistrarseViewModel model) { if (!ModelState.IsValid) { ModelState.AddModelError(string.Empty, "Hay datos inválidos"); return(View(model)); } var user = new IdentityUser { UserName = model.Email, Email = model.Email, }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { return(RedirectToAction("Login")); } else { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } return(View(model)); }
public async Task <ActionResult> Agregar(RegistrarseViewModel userViewModel, params string[] selectedGroups) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = userViewModel.Email, Email = userViewModel.Email, Nombre = userViewModel.Nombre, Apellido = userViewModel.Apellido, Estado = userViewModel.Estado }; var adminresult = await UserManager .CreateAsync(user, userViewModel.Password); //Add User to the selected Groups if (adminresult.Succeeded) { if (selectedGroups != null) { selectedGroups = selectedGroups ?? new string[] { }; await this.GroupManager .SetUserGroupsAsync(user.Id, selectedGroups); } var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmarEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirmar su cuenta", "Por favor para confirmar su cuenta haga click en el siguiente enlace: <a href=\"" + callbackUrl + "\">link</a>"); return(RedirectToAction("Index")); } else { AddErrors(adminresult); } } ViewBag.Groups = new SelectList( await RoleManager.Roles.ToListAsync(), "Id", "Name"); return(View()); }
public async Task <ActionResult> Registrarse(RegistrarseViewModel model) { model.Estado = true; if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Nombre = model.Nombre, Apellido = model.Apellido, Estado = model.Estado }; user.Calificacion = 0; user.CantServicios = 0; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await this.UserManager.AddToRoleAsync(user.Id, "Users"); string selectedGroups = "Users"; if (selectedGroups != null) { //selectedGroups = selectedGroups ?? new string[] { }; //await this.GroupManager // .SetUserGroupsAsync(user.Id, selectedGroups); } var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmarEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirmar su cuenta", "Por favor para confirmar su cuenta haga click en el siguiente enlace: <a href=\"" + callbackUrl + "\">link</a>"); //ViewBag.Link = callbackUrl; return(View("DisplayEmail")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Registrarse(RegistrarseViewModel model) { if (ModelState.IsValid) { var user = new IdentityUser { UserName = model.Email, Email = model.Email }; var result = await userManager.CreateAsync(user, model.Contrasenia); if (result.Succeeded) { await signInManager.SignInAsync(user, isPersistent : false); return(RedirectToAction("Index", "Home")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } } return(View(model)); }
public async Task <IActionResult> Registrarse(RegistrarseViewModel model) { if (!ModelState.IsValid) { throw new ExcepcionMuroAgil(ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage) .First()); } //Se elimina a los usuarios cuyas direcciones de correo no han sido confirmados. List <Usuario> usuariosSinConfirmar = _dbContext.Usuario .Where(u => u.TokenVerificador != null && u.TokenVerificador.Length > 0 && u.FechaCreacion.AddDays(1).CompareTo(DateTime.Now) <= 0) .ToList(); _dbContext.Usuario.RemoveRange(usuariosSinConfirmar); await _dbContext.SaveChangesAsync(); model.Codigo = model.Codigo.Trim(); model.Nombre = model.Nombre.Trim(); var targetUser = _dbContext.Usuario .SingleOrDefault(u => u.Correo.Equals(model.Codigo)); if (targetUser != null) { throw new ExcepcionMuroAgil("Ya existe un usuario con dicho nombre de usuario."); } if (model.Contrasenna.Length < 8) { throw new ExcepcionMuroAgil("La contraseña seleccionada debe tener 8 o más caracteres."); } if (!model.Contrasenna.Equals(model.RepContrasenna)) { throw new ExcepcionMuroAgil("Las contraseñas ingresadas no coinciden."); } byte[] randomBytes = new byte[72]; using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(randomBytes); } targetUser = new Usuario { Correo = model.Codigo, Nombre = model.Nombre, FechaCreacion = DateTime.Now, TokenVerificador = Convert.ToBase64String(randomBytes) }; var hasher = new PasswordHasher <Usuario>(); targetUser.HashContrasenna = hasher.HashPassword(targetUser, model.Contrasenna); await _dbContext.Usuario.AddAsync(targetUser); await _dbContext.SaveChangesAsync(); EnviarCorreoVerificador(targetUser); return(View("RegistroCompleto")); }