Ejemplo n.º 1
0
        /// <summary>
        /// Starts the password reset process for users who forgot their passwords.
        /// </summary>
        /// <param name="email">The email address of the user.</param>
        /// <param name="key">Out parameter for the unique key this user will temporarily be associated with.</param>
        /// <param name="role">The role: DJ or Mobile</param>
        /// <returns>The outcome of the operation.</returns>
        public Response StartPasswordReset(string email, string username, bool isDJ, string websiteAddress)
        {
            Response r = new Response();
            using (DatabaseConnectivity db = new DatabaseConnectivity())
            {
                // Try to establish a database connection
                r = db.OpenConnection();
                if (r.error)
                    return r;

                int ID;
                if (isDJ)
                {
                    r = db.DJValidateUsernameEmail(username, email, out ID);
                    if (r.error)
                        return r;
                }
                else
                {
                    r = db.MobileValidateUsernameEmail(username, email, out ID);
                    if (r.error)
                        return r;
                }

                if(ID == -1)
                {
                    r.error=true;
                    r.message="Username / email / Are you a DJ incorrect";
                    return r;
                }

                string random = Common.CreateSalt(32);
                Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                random = rgx.Replace(random, "x");
                int uniqueIsNegOne = 0;

                while (uniqueIsNegOne != -1)
                {
                    if (isDJ)
                    {
                        r = db.DJGetPasswordResetID(random, out uniqueIsNegOne);
                        if (r.error)
                            return r;
                    }
                    else
                    {
                        r = db.MobileGetPasswordResetID(random, out uniqueIsNegOne);
                        if (r.error)
                            return r;
                    }
                    random = Common.CreateSalt(32);
                    random = rgx.Replace(random, "x");
                }

                if (isDJ)
                {
                    r = db.DJSetPasswordReset(ID, random);
                    if (r.error)
                        return r;
                }
                else
                {
                    r = db.MobileSetPasswordReset(ID, random);
                    if (r.error)
                        return r;
                }

                try
                {
                    string resetURL = websiteAddress + "?DJ=" + isDJ.ToString() + "&key=" + random;
                    MailMessage mail = GeneratePasswordResetEmail(email, resetURL);
                    SmtpClient mailServer = new SmtpClient("smtp.live.com");
                    mailServer.Port = 25;
                    mailServer.UseDefaultCredentials = false;
                    mailServer.Credentials = new System.Net.NetworkCredential(mobiokeUsername, mobiokePassword);
                    mailServer.EnableSsl = true;
                    mailServer.Send(mail);
                    return r;
                }
                catch (Exception e)
                {
                    r.error = true;
                    r.message = "Exception in SendEmailWithUsername: " + e.Message;
                    return r;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Starts the password reset process for users who forgot their passwords.
        /// </summary>
        /// <param name="email">The email address of the user.</param>
        /// <param name="key">Out parameter for the unique key this user will temporarily be associated with.</param>
        /// <param name="role">The role: DJ or Mobile</param>
        /// <returns>The outcome of the operation.</returns>
        public Response StartPasswordReset(string email, string username, bool isDJ, string websiteAddress)
        {
            ExpResponse r = new ExpResponse();
            using (DatabaseConnectivity db = new DatabaseConnectivity())
            {
                // Try to establish a database connection
                r = db.OpenConnection();
                if (r.error)
                    return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web);

                int ID;
                if (isDJ)
                    r = db.DJValidateUsernameEmail(username, email, out ID);
                else
                    r = db.MobileValidateUsernameEmail(username, email, out ID);

                if (r.error)
                    return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web);

                if(ID == -1)
                {
                    r.setErMsg(true, Messages.MSG_EMAIL_NOT_FOUND);
                    return r;
                }

                string random = Common.CreateSalt(32);
                Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                random = rgx.Replace(random, "x");
                int uniqueIsNegOne = 0;

                while (uniqueIsNegOne != -1)
                {
                    if (isDJ)
                        r = db.DJGetPasswordResetID(random, out uniqueIsNegOne);
                    else
                        r = db.MobileGetPasswordResetID(random, out uniqueIsNegOne);

                    if(r.error)
                        return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web);

                    random = Common.CreateSalt(32);
                    random = rgx.Replace(random, "x");
                }

                if (isDJ)
                    r = db.DJSetPasswordReset(ID, random);
                else
                    r = db.MobileSetPasswordReset(ID, random);

                if(r.error)
                    return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web);

                try
                {
                    string resetURL = websiteAddress + "?DJ=" + isDJ.ToString() + "&key=" + random;
                    MailMessage mail = GeneratePasswordResetEmail(email, resetURL);
                    SmtpClient mailServer = new SmtpClient("smtp.live.com");
                    mailServer.Port = 25;
                    mailServer.UseDefaultCredentials = false;
                    mailServer.Credentials = new System.Net.NetworkCredential(Settings.EMAIL_ADR, Settings.EMAIL_PSWD);
                    mailServer.EnableSsl = true;
                    mailServer.Send(mail);
                    return r;
                }
                catch (Exception e)
                {
                    r.setErMsgStk(true, e.Message, e.StackTrace);
                    return Common.LogErrorRetNewMsg(r, Messages.ERR_EMAIL_SERVER, Common.LogFile.Web);
                }
            }
        }