示例#1
0
        public async Task <ActionResult> ForgotPassword(FormPost model, string returnUrl)
        {
            try {
                var formResults = model.resultsToDictionary();
                var email       = formResults["Email"].ToString();
                var user        = await UserManager.FindByNameAsync(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");
                    return(Json(new Payload(5, "InvalidForgotPasswordAttempt", model), JsonRequestBehavior.AllowGet));
                }
                // 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, email = user.Email, resetpassword = 1 }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

                //return RedirectToAction("ForgotPasswordConfirmation", "Account");
                return(Json(new Payload(1, "MODEL", "Forgot Password notification sent")));
            } catch (Exception e) {
                return(Json(new Payload(2, e, "An exception occurred trying to validate a forgotten password")));
            }
        }
示例#2
0
        public async Task <ActionResult> ResetPassword(FormPost model)
        {
            try {
                var formResults = model.resultsToDictionary();
                var code        = formResults["Code"].ToString();
                var password    = formResults["Password"].ToString();
                var email       = formResults["Email"].ToString();

                var user = await UserManager.FindByNameAsync(email);

                if (user == null)
                {
                    // Don't reveal that the user does not exist
                    //return RedirectToAction("ResetPasswordConfirmation", "Account");
                    return(Json(new Payload(5, "InvalidResetPasswordAttempt", model), JsonRequestBehavior.AllowGet));
                }
                var result = await UserManager.ResetPasswordAsync(user.Id, code, password);

                if (result.Succeeded)
                {
                    //return RedirectToAction("ResetPasswordConfirmation", "Account");
                    return(Json(new Payload(1, "MODEL", "Password reset success")));
                }
                else
                {
                    return(Json(new Payload(2, "MODEL", "Password reset failed")));
                }
            } catch (Exception e) {
                return(Json(new Payload(2, e, "An exception occurred trying to reset a password")));
            }
            //AddErrors(result);
            //return View();
        }
示例#3
0
            /// <summary>
            /// Custom implementation of ChallengeResult for SPA
            /// </summary>
            /// <param name="formPost"></param>
            public ChallengeResult(FormPost formPost)
            {
                Dictionary <string, object> results = formPost.resultsToDictionary();

                LoginProvider = results["provider"].ToString();
                RedirectUri   = results["redirectUri"].ToString();
                UserId        = results["userId"].ToString();
            }
示例#4
0
        public async Task <ActionResult> Register(FormPost formPost)
        {
            try {
                var formResults = formPost.resultsToDictionary();
                var user        = new ApplicationUser {
                    UserName = formResults["Email"].ToString(),
                    Email    = formResults["Email"].ToString()
                };
                var result = await UserManager.CreateAsync(user, formResults["Password"].ToString());

                if (result.Succeeded)
                {
                    //Assign Role to user Here
                    await this.UserManager.AddToRoleAsync(user.Id, "User");

                    //Ends Here

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // 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);

                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    //return RedirectToAction("Index", "Home");
                    return(Json(new Payload(2, "Notification", result, "An email to confirm your account has been sent.")));
                }
                else
                {
                    AddErrors(result);
                    return(Json(new Payload(2, "Error", result, "Failed to create user.  See Results for details.")));
                }
            } catch (Exception e) {
                return(Json(new Payload(2, e, "An exception occurred trying to register a user.")));
            }

            // If we got this far, something failed, redisplay form
            //return View(model);
            return(Json(new Payload(2, "Error", new MODEL(), "Something went wrong.")));
        }
示例#5
0
        [HttpPost, AllowAnonymous, ValidateAntiForgeryToken]   //ValidateAntiForgeryToken
        public async Task <ActionResult> Login(FormPost model) //string returnUrl
        {
            try {
                var formResults = model.resultsToDictionary();
                // 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(
                    formResults["Email"].ToString(),
                    formResults["Password"].ToString(),
                    formResults.ContainsKey("RememberMe"), // checkbox
                    shouldLockout : false
                    );

                switch (result)
                {
                case SignInStatus.Success:
                    //return RedirectToLocal(returnUrl);
                    return(Json(new Payload(1, "MODEL", result, "Successfully logged in")));

                case SignInStatus.LockedOut:
                    //return View("Lockout");
                    return(Json(new Payload(2, "Lockout", model), JsonRequestBehavior.AllowGet));

                case SignInStatus.RequiresVerification:
                    /*return RedirectToAction("SendCode", new {
                     *  ReturnUrl = returnUrl,
                     *  RememberMe = true // formResults["RememberMe"].ToString() //model.RememberMe
                     * });*/
                    //model.Add("RememberMe", true);
                    return(Json(new Payload(3, "SendCode", model), JsonRequestBehavior.AllowGet));

                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return(Json(new Payload(4, "InvalidLoginAttempt", model), JsonRequestBehavior.AllowGet));
                    //return View(model);
                }
            } catch (Exception e) {
                return(Json(new Payload(2, e, "An exception occurred trying to log in")));
            }
        }