コード例 #1
0
        public ActionResult ResetPwdByPhone(string username, string token, LocalPasswordModel model)
        {
            this.ViewBag.username = username;
            this.ViewBag.token = token;
            // 用户没有本地密码,因此将删除由于缺少
            // OldPassword 字段而导致的所有验证错误
            var state = this.ModelState["OldPassword"];
            if (state != null)
            {
                state.Errors.Clear();
            }

            if (string.IsNullOrEmpty(username))
                this.ModelState.AddModelError("", "username can't null ");
            if (string.IsNullOrEmpty(token))
                this.ModelState.AddModelError("", "token can't null");


            if (this.ModelState.IsValid)
            {
                var bs = this._userService.ResetPasswordByPhoneToken(token, model.NewPassword, username);

                if (bs.ErrorCode == 0)
                {
                    return this.Message("重置成功", this.Url.Action("Login", "Cas"));
                }
                else
                {
                    this.ModelState.AddModelError("", bs.ErrorMessage);
                }
            }

            return this.View();
        }
コード例 #2
0
        public ActionResult RestPwd(string token, LocalPasswordModel model)
        {
            // 用户没有本地密码,因此将删除由于缺少
            // OldPassword 字段而导致的所有验证错误
            var state = this.ModelState["OldPassword"];
            if (state != null)
            {
                state.Errors.Clear();
            }

            if (this.ModelState.IsValid)
            {
                var isok =
                    this._userService.ResetPasswordByEmailToken(token, NewPassword: model.NewPassword);

                if (isok)
                    return this.Message("重置密码成功", this.Url.Action("login", "cas"));
                else
                    return this.Message("重置密码失败", this.Url.Action("RestPwd", new { token }));
            }
            return View(model);
        }
コード例 #3
0
        public ActionResult Manage(LocalPasswordModel model)
        {
            var hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(this.User.Identity.Name));
            this.ViewBag.HasLocalPassword = hasLocalAccount;
            this.ViewBag.ReturnUrl = this.Url.Action("Manage");
            if (hasLocalAccount)
            {
                if (this.ModelState.IsValid)
                {
                    // 在某些失败方案中,ChangePassword 将引发异常,而不是返回 false。
                    bool changePasswordSucceeded;
                    try
                    {
                        changePasswordSucceeded = WebSecurity.ChangePassword(this.User.Identity.Name, model.OldPassword,
                                                                             model.NewPassword);
                    }
                    catch (Exception)
                    {
                        changePasswordSucceeded = false;
                    }

                    if (changePasswordSucceeded)
                    {
                        return this.RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        this.ModelState.AddModelError("", "当前密码不正确或新密码无效。");
                    }
                }
            }
            else
            {
                // 用户没有本地密码,因此将删除由于缺少
                // OldPassword 字段而导致的所有验证错误
                var state = this.ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (this.ModelState.IsValid)
                {
                    try
                    {
                        WebSecurity.CreateAccount(this.User.Identity.Name, model.NewPassword);
                        return this.RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    catch (Exception e)
                    {
                        this.ModelState.AddModelError("", e);
                    }
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }