⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accountcontroller.cs

📁 人物传记/成功经验人物传记/成功经验人物传记/成功经验人物传记/成功经验人物传记/成功经验
💻 CS
📖 第 1 页 / 共 2 页
字号:
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult RegisterBefore(bool Checked)
        {
            if (!Checked) return RedirectToAction("Login");
            return RedirectToAction("Register");
        }
        #endregion

        #region =========Auditing============
        public ActionResult Auditing()
        {
            if (Request.IsAuthenticated)
            {
                HttpContext.Profile.SetPropertyValue("Tele", Request.QueryString["Tele"]);
                HttpContext.Profile.SetPropertyValue("Mobile", Request.QueryString["Mobile"]);
                HttpContext.Profile.SetPropertyValue("Corporation", Request.QueryString["Corporation"]);
                HttpContext.Profile.SetPropertyValue("RealityName", Request.QueryString["RealityName"]);
                HttpContext.Profile.SetPropertyValue("IsAudited", Convert.ToBoolean(Request.QueryString["IsAudited"]));
                HttpContext.Profile.SetPropertyValue("IsStoped", Convert.ToBoolean(Request.QueryString["IsStoped"]));
                HttpContext.Profile.Save();
            }
            return View();
        }
        #endregion

        #region ==========FindPassWordByEmail===========

        public ActionResult FindPassWordByEmail()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FindPassWordByEmail(string UserName, string Email)
        {
            if (Membership.GetUser(UserName) == null)
            {
                ModelState.AddModelError("_FORM", "用户名不存在!.");
                return View();
            }
            if (Membership.FindUsersByEmail(Email) == null)
            {
                ModelState.AddModelError("_FORM", "此Email不存在,请输入正确的Email!.");
                return View();
            }
            if (Membership.FindUsersByEmail(Email).Count > 0)
            {
                ModelState.AddModelError("_FORM", "此Email不存在,请输入正确的Email!.");
                return View();
            }
            if (Membership.GetUser(UserName).Email.ToLower() != Email.ToLower())
            {
                ModelState.AddModelError("_FORM", "此Email不是您的,请输入正确的Email");
                return View();
            }

            string password = SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(Membership.GetUser(User).GetPassword());
            string body = "您的密码是: " + password;
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "Files/Admin/Email.xml";
            SkyiSite.DBUtility.Email.Instance.SendMail(Email, "找回密码", body, filePath);
            //SendMail("gxsme@163.com", Email, "你的密码", "你的密码是:" + Server.HtmlEncode(SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(password)), "gxsme@163.com", "sme.gxsti");

            return RedirectToAction("Empty", new { id = SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String("已将密码发送到您的邮箱,请注意查收!") });
        }
        #endregion

        #region ==========LogOff==========
        public ActionResult LogOff()
        {
            FormsAuth.SignOut();
            return RedirectToAction("Login");
        }
        #endregion

        #region ===========ChangePassword ===========
        [Authorize]
        public ActionResult ChangePassword()
        {
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChangePassword(string currentPassword, string newPassword, string confirmPassword)
        {
            try
            {
                if (Membership.GetUser().ChangePassword(SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(currentPassword), SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(newPassword)))
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
                    ModelState.AddModelError("_FORM", "The current password is incorrect or the new password is invalid.");
                    return View();
                }
            }
            catch
            {
                ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
                ModelState.AddModelError("_FORM", "The current password is incorrect or the new password is invalid.");
                return View();
            }
        }
        #endregion

        #region ==========ChangePasswordSuccess==========
        public ActionResult ChangePasswordSuccess()
        {

            return View();
        }
        #endregion

        #region ==========系统自带的方法==========
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity is WindowsIdentity)
            {
                throw new InvalidOperationException("Windows authentication is not supported.");
            }
        }


        private bool ValidateChangePassword(string currentPassword, string newPassword, string confirmPassword)
        {
            if (String.IsNullOrEmpty(currentPassword))
            {
                ModelState.AddModelError("currentPassword", "You must specify a current password.");
            }
            if (newPassword == null || newPassword.Length < MembershipService.MinPasswordLength)
            {
                ModelState.AddModelError("newPassword",
                    String.Format(CultureInfo.CurrentCulture,
                         "You must specify a new password of {0} or more characters.",
                         MembershipService.MinPasswordLength));
            }

            if (!String.Equals(newPassword, confirmPassword, StringComparison.Ordinal))
            {
                ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
            }

            return ModelState.IsValid;
        }

        private bool ValidateLogOn(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName))
            {
                ModelState.AddModelError("username", "用户名不能为空.");
            }
            if (String.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("password", "密码不能为空.");
            }
            if (!MembershipService.ValidateUser(userName, password))
            {
                ModelState.AddModelError("_FORM", "请输入正确的用户名和密码.");
            }

            return ModelState.IsValid;
        }



        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://msdn.microsoft.com/en-us/library/system.web.security.membershipcreatestatus.aspx for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "Username already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A username for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion

    }

    #region ==========系统自带的类,方便操作==========
    // The FormsAuthentication type is sealed and contains static members, so it is difficult to
    // unit test code that calls its members. The interface and helper class below demonstrate
    // how to create an abstract wrapper around such a type in order to make the AccountController
    // code unit testable.

    public interface IFormsAuthentication
    {
        void SignIn(string userName, bool createPersistentCookie);
        void SignOut();
    }

    public class FormsAuthenticationService : IFormsAuthentication
    {
        public void SignIn(string userName, bool createPersistentCookie)
        {
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);//, ".ASPXAUTH");
        }
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }

    public interface IMembershipService
    {
        int MinPasswordLength { get; }

        bool ValidateUser(string userName, string password);
        MembershipCreateStatus CreateUser(string userName, string password, string email);
        bool ChangePassword(string userName, string oldPassword, string newPassword);
    }

    public class AccountMembershipService : IMembershipService
    {
        private MembershipProvider _provider;

        public AccountMembershipService()
            : this(null)
        {
        }

        public AccountMembershipService(MembershipProvider provider)
        {
            _provider = provider ?? Membership.Provider;
        }

        public int MinPasswordLength
        {
            get
            {
                return _provider.MinRequiredPasswordLength;
            }
        }

        public bool ValidateUser(string userName, string password)
        {
            return _provider.ValidateUser(userName, password);
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            _provider.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
            return currentUser.ChangePassword(oldPassword, newPassword);
        }
    #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -