Exemple #1
0
        public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByNameAsync(model.Email);

                if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return(View("ForgotPasswordConfirmation"));
                }

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                string body = "We have processed your forgotten password request from the Notification Portal. Reset your password by clicking the link below.";

                await UserManager.SendEmailAsync(user.Id, "Reset Password", TemplateService.AccountEmail(callbackUrl, body, "Reset Password"));

                return(RedirectToAction("ForgotPasswordConfirmation", "Account"));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemple #2
0
        public async Task <ActionResult> Create(AddUserVM model)
        {
            // If success then proceed
            if (ModelState.IsValid)
            {
                // If the method returns true from user repo, do this
                if (_userRepo.AddUser(model, out string msg, out string userId))
                {
                    // Generate the email confirmation code for the user being generated
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(userId);

                    // Generating the call back url which will be sent to the user for confirmation
                    var callbackUrl = Url.Action("ConfirmEmail", "User", new { userId = userId, code = code }, protocol: Request.Url.Scheme);

                    // This is the content for the body of confirmation email message
                    string body = "Welcome to Notification Portal. In order to get started, you need to confirm your email address.";

                    // Send the confirmation email with TemplateService -> AccountEmail method
                    await UserManager.SendEmailAsync(userId, "Confirm your account", TemplateService.AccountEmail(callbackUrl, body, "Confirm Email"));

                    TempData["SuccessMsg"] = msg;
                    return(RedirectToAction("Index"));
                }
                TempData["ErrorMsg"] = msg;
            }

            // Load the dropdown / selectlistrepo values if adding the user fails
            model = _userRepo.CreateAddUserVM(model);
            // if model returns null, redirect to index
            if (model == null)
            {
                TempData["ErrorMsg"] = "Cannot create new user at the moment";
                return(RedirectToAction("Index"));
            }

            return(View(model));
        }