예제 #1
0
        public async Task <IActionResult> Create(RegisterNewViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    Address     = model.Address,
                    Email       = model.UserName,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    UserName    = model.UserName
                };

                var response = await _userHelper.AddUserAsync(user, model.Password);

                if (response.Succeeded)
                {
                    var userInDB = await _userHelper.GetUserByEmailAsync(model.UserName);

                    await _userHelper.AddUSerToRoleAsync(userInDB, "Customer");

                    var creator = await _userHelper.GetUserByEmailAsync(User.Identity.Name);

                    var owner = new Owner
                    {
                        Appointments = new List <Appointment>(),
                        Pets         = new List <Pet>(),
                        User         = userInDB,
                        CreatedBy    = creator,
                        ModifiedBy   = creator,
                        CreateDate   = DateTime.Now,
                        UpdateDate   = DateTime.Now
                    };

                    await _ownerRepository.CreateAsync(owner);

                    try
                    {
                        var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(userInDB);

                        var tokenLink = Url.Action("ConfirmEmail", "Account", new
                        {
                            userid = userInDB.Id,
                            token  = myToken
                        }, protocol: HttpContext.Request.Scheme);

                        _mailHelper.SendMail(model.UserName, "Email confirmation", $"<h1>Email Confirmation</h1>" +
                                             $"To allow the user, " +
                                             $"please click in this link:</br></br><a href = \"{tokenLink}\">Confirm Email</a>");

                        return(RedirectToAction(nameof(Index)));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.ToString());
                        return(View(model));
                    }
                }

                ModelState.AddModelError(string.Empty, response.Errors.FirstOrDefault().Description);
            }

            return(View(model));
        }
예제 #2
0
        public async Task <IActionResult> Register(RegisterNewViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Buscarmos el email para ver si ya existe algun usuario con ese correo
                var user = await _userHelper.FindUserByEmailAsync(model.Username);

                if (user == null) //Verificamos si no encontro un usuario con ese correo para seguir
                {
                    //TODO:
                    user = new User
                    {
                        Email    = model.Username,
                        UserName = model.Username,
                        Name     = model.FirstName,
                        LastName = model.LastName
                    };

                    //Creamos el usuario
                    var result = await _userHelper.CreateUserAsync(user, model.Password);

                    //Verificamos que se haya creado el usuario, de lo contrario mandamos un mensaje a la vista
                    if (result != IdentityResult.Success)
                    {
                        this.ModelState.AddModelError(string.Empty, "Could not be created.");
                        return(View(model));
                    }

                    //Ante  logeabamos el usuario, ahora debe confirmar via email
                    var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                    var tokenLink = Url.Action("ConfirmEmail", "Account", new
                    {
                        userid = user.Id,
                        token  = myToken
                    }, protocol: HttpContext.Request.Scheme);

                    var msj = $"<h1>Email Confirmation</h1> To allow the user please click in this link:</br></br><a href={tokenLink}>Confirm Email </a>";

                    _mailHelper.SendMail(model.Username, "Email Confirmation", msj);
                    ViewBag.Message = "The instuction to allow you user has been sent to email.";
                    return(View(model));

                    #region Loguear desde que se registre

                    /* //Aqui inicia par loguear el usuario lo puse en comentario porque ahora estamos usando confirmacion de correo, no estamos logueando de una vez que se regustre
                     * //Luego de que el usuario es creado procedemos a iniciar session, pasamos los datos a loginViewModel para enviarselo al metdo login
                     * var loginVM = new LoginViewModel
                     * {
                     *  Username = model.Username,
                     *  Password = model.Password,
                     *  RememberMe = false
                     * };
                     *
                     * //Enviamos los datos al metodo  login para iniciar session
                     * var result2 = await _userHelper.LoginAsync(loginVM);
                     *
                     * //Si los datos son correcto, ya logueado nos vamos para la pantalla principal
                     * if(result2.Succeeded)
                     * {
                     *  return RedirectToAction(nameof(HomeController.Index), "Home");
                     * }
                     *
                     * //Es imposible de que llegue a esta parte, ya que si el usuario fue creado con exito, lo va a loggear
                     * this.ModelState.AddModelError(string.Empty, "Could not be login.");
                     * return View(model);
                     */
                    #endregion
                }

                ModelState.AddModelError(string.Empty, "The username is alredy registerd.");
            }

            return(View(model));
        }
예제 #3
0
        public async Task <IActionResult> Register(RegisterNewViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userHelper.GetUserByEmailAsync(model.UserName);

                if (user == null)
                {
                    user = new User
                    {
                        FirstName   = model.FirstName,
                        LastName    = model.LastName,
                        Email       = model.UserName,
                        UserName    = model.UserName,
                        Address     = model.Address,
                        PhoneNumber = model.PhoneNumber
                    };
                }

                var result = await _userHelper.AddUserAsync(user, model.Password);

                if (result != IdentityResult.Success)
                {
                    ModelState.AddModelError(string.Empty, "User couldn't be created.");
                    return(View(model));
                }

                var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                var tokenLink = Url.Action("ConfirmEmail", "Account", new
                {
                    userid = user.Id,
                    token  = myToken
                }, protocol: HttpContext.Request.Scheme);

                _mailHelper.SendMail(model.UserName, "Email confirmation",
                                     $" <td style = 'background-color: #ecf0f1'>" +
                                     $"      <div style = 'color: #34495e; margin: 4% 10% 2%; text-align: justify;font-family: sans-serif'>" +
                                     $"            <h1 style = 'color: #e67e22; margin: 0 0 7px' > Hello, welcome </h1>" +
                                     $"                    <p style = 'margin: 2px; font-size: 15px'>" +
                                     $"                      The best specialized Veterinary Clinic in Lisbon focused on providing medical and surgical services<br>" +
                                     $"                      applying the most current techniques for accurate diagnoses and timely treatments..<br>" +
                                     $"                      Among the services we have:</p>" +
                                     $"      <ul style = 'font-size: 15px;  margin: 10px 0'>" +
                                     $"        <li> Emergencies.</li>" +
                                     $"        <li> Internal medicine.</li>" +
                                     $"        <li> Radiology.</li>" +
                                     $"        <li> Laboratory and cabinet tests.</li>" +
                                     $"      </ul>" +
                                     $"  <div style = 'width: 100%;margin:20px 0; display: inline-block;text-align: center'>" +
                                     $"  </div>" +
                                     $"  <div style = 'width: 100%; text-align: center'>" +
                                     $"    <h2 style = 'color: #e67e22; margin: 0 0 7px' >Email Confirmation </h2>" +
                                     $"    To allow the user, please click in this link:</br></br> " +
                                     $"    <a style ='text-decoration: none; border-radius: 5px; padding: 11px 23px; color: white; background-color: #3498db' href = \"{tokenLink}\">Confirm Email</a>" +
                                     $"    <p style = 'color: #b3b3b3; font-size: 12px; text-align: center;margin: 30px 0 0' > Joana Veterinary Clinic 2020 </p>" +
                                     $"  </div>" +
                                     $" </td >" +
                                     $"</tr>" +
                                     $"</table>");

                ViewBag.Message = "The instructions to allow your user has been sent to email.";

                return(View(model));
            }

            ModelState.AddModelError(string.Empty, "This username is already registered.");

            return(View(model));
        }