Beispiel #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);

                string callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                try
                {
                    var    emailModel = new CallbackEmailModel(callbackUrl, user.Mailbox);
                    string body       = EmailController.RenderViewToString("Email", "ForgotPassword", emailModel).Trim();
                    await UserManager.SendEmailAsync(user.Id, "Reset your password", body);
                }
                catch
                {
                    AuthenticationManager.SignOut();
                    ViewBag.NotLoggedIn = true;
                    return(View("Error"));
                }
                return(RedirectToAction("ForgotPasswordConfirmation", "Account"));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #2
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, true, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
            {
                var currentUser = UserManager.FindByNameAsync(model.Email);
                if (!await UserManager.IsEmailConfirmedAsync(currentUser.Result.Id))
                {
                    string mailbox = currentUser.Result.Mailbox;
                    AuthenticationManager.SignOut();

                    // Send email
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(currentUser.Result.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = currentUser.Result.Id, code = code }, protocol: Request.Url.Scheme);
                    try
                    {
                        var    emailModel = new CallbackEmailModel(callbackUrl, mailbox);
                        string body       = EmailController.RenderViewToString("Email", "ConfirmAccount", emailModel).Trim();
                        await UserManager.SendEmailAsync(currentUser.Result.Id, "Confirm your account", body);
                    }
                    catch
                    {
                        AuthenticationManager.SignOut();
                        ViewBag.NotLoggedIn = true;
                        return(View("Error"));
                    }
                    finally { }
                    // Show message
                    AuthenticationManager.SignOut();
                    ViewBag.NotLoggedIn = true;
                    return(RedirectToAction("DisplayEmail"));
                }
                return(RedirectToLocal(returnUrl));
            }

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (NXtelData.Options.DisableAccountRegistration)
            {
                return(View("Error"));
            }
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                user.Mailbox = NXtelData.User.GetUniqueMailbox();
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var u = NXtelData.User.LoadByUserName(model.Email);
                    u.Mailbox = user.Mailbox;
                    string err;
                    NXtelData.User.Save(u, out err);
                    AuthenticationManager.SignOut();

                    // 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.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    try
                    {
                        var    emailModel = new CallbackEmailModel(callbackUrl, u.Mailbox);
                        string body       = EmailController.RenderViewToString("Email", "ConfirmAccount", emailModel).Trim();
                        await UserManager.SendEmailAsync(user.Id, "Confirm your account", body);
                    }
                    catch
                    {
                        AuthenticationManager.SignOut();
                        ViewBag.NotLoggedIn = true;
                        return(View("Error"));
                    }
                    ViewBag.NotLoggedIn = true;
                    return(View("DisplayEmail"));
                }
                AddErrors(result);
            }

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