Carrotware.CMS.Core.ProfileManager.ResetPassword C# (CSharp) Метод

ResetPassword() публичный Метод

public ResetPassword ( string Email, Control theControl ) : bool
Email string
theControl System.Web.UI.Control
Результат bool
        public bool ResetPassword(string Email, Control theControl)
        {
            MembershipUser user = null;

            if (!String.IsNullOrEmpty(Email)) {
                MembershipUserCollection membershipCollection = Membership.FindUsersByEmail(Email);
                foreach (MembershipUser userEnum in membershipCollection) {
                    user = userEnum;
                    break;
                }
            }

            if (user != null) {
                HttpRequest request = HttpContext.Current.Request;
                Assembly _assembly = Assembly.GetExecutingAssembly();

                string sBody = String.Empty;
                using (StreamReader oTextStream = new StreamReader(_assembly.GetManifestResourceStream("Carrotware.CMS.Core.Security.EmailForgotPassMsg.txt"))) {
                    sBody = oTextStream.ReadToEnd();
                }

                if (user.IsLockedOut && user.LastLockoutDate < DateTime.Now.AddMinutes(-45)) {
                    user.UnlockUser();
                }

                string tmpPassword = user.ResetPassword(); // set to known password
                string newPassword = GenerateSimplePassword(); // create simpler password

                user.ChangePassword(tmpPassword, newPassword); // set to simpler password

                string strHTTPHost = String.Empty;
                try { strHTTPHost = request.ServerVariables["HTTP_HOST"].ToString().Trim(); } catch { strHTTPHost = String.Empty; }

                string hostName = strHTTPHost.ToLowerInvariant();

                string strHTTPPrefix = "http://";
                try {
                    strHTTPPrefix = request.ServerVariables["SERVER_PORT_SECURE"] == "1" ? "https://" : "http://";
                } catch { strHTTPPrefix = "http://"; }

                strHTTPHost = String.Format("{0}{1}", strHTTPPrefix, strHTTPHost).ToLowerInvariant();

                sBody = sBody.Replace("{%%UserName%%}", user.UserName);
                sBody = sBody.Replace("{%%Password%%}", newPassword);
                sBody = sBody.Replace("{%%SiteURL%%}", strHTTPHost);
                sBody = sBody.Replace("{%%Version%%}", CurrentDLLVersion);
                sBody = sBody.Replace("{%%AdminFolderPath%%}", String.Format("{0}{1}", strHTTPHost, SiteData.AdminFolderPath));

                if (SiteData.CurretSiteExists) {
                    sBody = sBody.Replace("{%%Time%%}", SiteData.CurrentSite.Now.ToString());
                } else {
                    sBody = sBody.Replace("{%%Time%%}", DateTime.Now.ToString());
                }

                EmailHelper.SendMail(null, user.Email, String.Format("Reset Password {0}", hostName), sBody, false);

                return true;
            } else {
                return false;
            }
        }

Usage Example

Пример #1
0
        protected void cmdReset_Click(object sender, EventArgs e)
        {
            ProfileManager p = new ProfileManager();
            bool bReset = false;
            lblErr.Text = "";
            FailureText.Text = "";
            divErrMsg.Visible = false;

            try { bReset = p.ResetPassword(txtEmail.Text, this); } catch (Exception ex) { lblErr.Text = ex.ToString(); }
            //bReset = p.ResetPassword(txtEmail.Text, this);

            if (bReset) {
                InfoMessage.Text = "Email sent with new password.";
            } else {
                if (lblErr.Text.ToLower().Contains("system.net.mail.smtpclient")
                        || lblErr.Text.ToLower().Contains("system.net.mime.mailbnfhelper.readmailaddress")
                        || lblErr.Text.ToLower().Contains("system.net.mail.mailaddresscollection")
                        || lblErr.Text.ToLower().Contains("system.security.securityexception")) {
                    FailureText.Text = "Error sending reset message.";
                } else {
                    FailureText.Text = "Invalid username/email.";
                }
            }
            divLogonLink.Visible = bReset;

            SetMsgVisible();

            txtEmail.Text = "";
        }