Exemplo n.º 1
0
        public async Task<IActionResult> InvitationRequest(InvitationViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = model.Email,
                    Email = model.Email,
                    Address = model.WorkAddress,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    City = model.City,
                    DOB = model.DOB,
                    Phone = model.Phone,
                    Zip = model.Zip
                };

                var appUser = await _userManager.FindByNameAsync(model.Email);
                if (appUser != null)
                {
                    ModelState.AddModelError(string.Empty, "Sorry. This email is already registered");
                    return View(model);
                }

                var message = new AuthMessageSender.Message
                {
                    Subject = "Register User",
                    Body = "Please add me: <br>" +
                           "First Name: " + user.FirstName + "<br>" +
                           "Last Name: " + user.LastName + "<br>" +
                           "DOB: " + user.DOB.ToString("d") + "<br>" +
                           "Phone: " + user.Phone + "<br>" +
                           "Email: " + user.Email + "<br>" +
                           "Address: " + user.Address + "<br>" +
                           "City: " + user.City + "<br>" +
                           "Zip: " + user.Zip + "<br>"
                };
                
                await _emailSender.SendEmailAsync(_userEmailAccount, message.Subject, message.Body, _userEmailAccount,
                    _userEmailPassword);

                return View("ThankYou");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemplo n.º 2
0
        public async Task<IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = model.Email,
                    Email = model.Email,
                    Address = model.WorkAddress,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    City = model.City,
                    DOB = model.DOB,
                    Phone = model.Phone,
                    Zip = model.Zip
                };

                var result = await _userManager.CreateAsync(user, model.Password);
              
                if (result.Succeeded)
                {
                    // Send an email with this link
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, Request.Scheme);

                    var message = new AuthMessageSender.Message                   
                    {
                        Subject = "Confirm account",
                        Body = "Please confirm " + user.FirstName + " " + user.LastName + " account by clicking: <a href='" + callbackUrl + "'>this link</a></br>" + 
                               "You can login using the following password: "******"</br>" + 
                               "Please, once logged in change your password for security reasons. Enjoy"
                    };

                    await _emailSender.SendEmailAsync(model.Email, message.Subject, message.Body, _userEmailAccount,
                        _userEmailPassword);

                    message.Body = "You have been registered into 3DCytoFlow! Please, check your email to confirm your account";

                    _smsSender.SendSms(message, _sid, _authToken, _number, user.Phone);

                    await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    return RedirectToAction(nameof(HomeController.Index), "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemplo n.º 3
0
        public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByNameAsync(model.Email);
                if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return View("ForgotPasswordConfirmation");
                }

                // Send an email with this link
                var code = await _userManager.GeneratePasswordResetTokenAsync(user);
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code }, Request.Scheme);

                var message = new AuthMessageSender.Message
                {
                    Subject = "Reset Password",
                    Body = "Please reset your password by clicking:  <a href='" + callbackUrl + "'>this link</a>"
                };

                await _emailSender.SendEmailAsync(model.Email, message.Subject, message.Body, _userEmailAccount,
                    _userEmailPassword);

                return View("ForgotPasswordConfirmation");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemplo n.º 4
0
        public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var user = await _userManager.FindByNameAsync(model.Email);
                if (user == null)
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return View(model);
                }
                if (!await _userManager.IsEmailConfirmedAsync(user))
                {
                    ViewBag.errorMessage = "Your email has not been confirmed. If you did not receive the email, another is in its way to: " + model.Email;

                    // Send an email with this link
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, Request.Scheme);

                    var message = new AuthMessageSender.Message
                    {
                        Subject = "Confirm account",
                        Body = "Please confirm " + user.FirstName + " " + user.LastName + " account by clicking: <a href='" + callbackUrl + "'>this link</a></br>" +
                               "You can login using the following password: "******"</br>" +
                               "Please, once logged in change your password for security reasons. Enjoy"
                    };

                    await _emailSender.SendEmailAsync(model.Email, message.Subject, message.Body, _userEmailAccount,
                        _userEmailPassword);

                    return View("Error");
                }

                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);

                if (result.Succeeded)
                {
                    _logger.LogInformation(1, "User logged in.");
                    return RedirectToAction("Instructions", "Home");
                }
                if (result.RequiresTwoFactor)
                {
                    return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning(2, "User account locked out.");
                    return View("Lockout");
                }

                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return View(model);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }