示例#1
0
        public string Signup(Login login)
        {
            string   hash = Logdata.GetHash(login.PW);
            LogEntry log  = new LogEntry("Signup", login.Name);


            MailAddress emailAddr;
            string      result = "OK, now please wait for an email and click the link to complete your registration";

            try
            {
                emailAddr = new MailAddress(login.Email);
                // Valid address
            }
            catch
            {
                return("This email address appears to be invalid");
            }
            if (login.PW.Length < 4 || login.PW.Length > 10)
            {
                return("Password must be between 4 and 10 characters");
            }



            if (gpxConnection.IsConnect())
            {
                // check username and email
                string query = "SELECT Id, name, pw, email FROM logins";
                try
                {
                    using (MySqlDataAdapter loginAdapter = new MySqlDataAdapter(query, gpxConnection.Connection))
                    {
                        dataLogins = new DataTable();
                        loginAdapter.Fill(dataLogins);

                        int length = dataLogins.Rows.Count;
                        for (int row = 0; row < length; row++)
                        {
                            DataRow dr     = dataLogins.Rows[row];
                            string  dbname = (string)dr["name"];
                            dbname = dbname.Trim();
                            string dbpw = (string)dr["pw"];
                            dbpw = dbpw.Trim();
                            string dbemail = (string)dr["email"];
                            dbemail = dbemail.Trim();
                            if (dbname.ToLower() == login.Name.ToLower())
                            {
                                return("Sorry, this username has already been taken");
                            }
                            if (dbemail == login.Email)
                            {
                                return("Sorry, only one login allowed per email address");
                            }
                            if (EmailConnection.IsValidEmail(dbemail) == false)
                            {
                                return("Sorry, this email doesn't appear to be valid");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error = ex.Message;
                    return("DB error: " + ex.Message);
                }

                // create and send an email
                try
                {
                    // create a code based on data
                    login.EmailCode = Logdata.GetHash(login.Name + login.Name);

                    string URLstr = string.Format(Connections.serviceURL + "?user={0}&regcode={1}", login.Name, login.EmailCode);
                    //string URLstr = string.Format("http://localhost/routes/www?user={0}&regcode={1}",login.Name, login.EmailCode);

                    EmailConnection ec      = new EmailConnection();
                    MailAddress     from    = new MailAddress("*****@*****.**");
                    MailMessage     message = new MailMessage(from, emailAddr)
                    {
                        Subject = "TCC rides signup",
                        Body    = string.Format("Please click {0}  to complete your registration", URLstr)
                    };

                    try
                    {
                        SmtpClient client = new System.Net.Mail.SmtpClient(ec.Server)
                        {
                            Credentials = new System.Net.NetworkCredential(ec.User, ec.PW)
                        };
                        client.Send(message);

                        // save the login details but with role as zero so login won't yet work
                        log   = new LogEntry("Register1", login.Name + " " + login.EmailCode);
                        query = string.Format("insert into logins (name, pw, email,role,messagetime,units,climbs) values ('{0}','{1}','{2}',{3},'{4}','{5}',{6})",
                                              login.Name, hash, login.Email, 0, Logdata.DBTimeString(DateTime.Now), login.Units, login.Climbs);

                        try
                        {
                            var cmd = new MySqlCommand(query, gpxConnection.Connection);
                            cmd.ExecuteNonQuery();
                            result = "Thank you, please wait for an email and click link to complete registration";
                        }
                        catch (Exception ex2)
                        {
                            result = "There is a database error, please try again:" + ex2.Message;;
                        }
                    }
                    catch (Exception ex)
                    {
                        result = "Sorry, there is an error with the email service: " + ex.Message;
                    }
                }
                catch (Exception ex2)
                {
                    result    = "Error: " + ex2.Message;
                    log.Error = ex2.Message;
                }
                finally
                {
                    log.Result = result;
                    log.Save(gpxConnection);
                    gpxConnection.Close();
                }
                return(result);
            }
            else
            {
                return(DBConnection.ErrStr);
            }
        }
示例#2
0
        public string ForgetPassword(string email)
        {
            LogEntry log = new LogEntry("ForgetPassword", email);

            string      result   = "OK, now please wait for an email and click the link to set a new password";
            string      username = "";
            MailAddress emailAddr;

            try
            {
                emailAddr = new MailAddress(email);
                // Valid address
            }
            catch
            {
                return("This email address appears to be invalid");
            }

            if (gpxConnection.IsConnect())
            {
                string query = string.Format("SELECT Id, name, email FROM logins where email = '{0}'", email);
                try
                {
                    using (MySqlDataAdapter loginAdapter = new MySqlDataAdapter(query, gpxConnection.Connection))
                    {
                        dataLogins = new DataTable();
                        loginAdapter.Fill(dataLogins);
                        int count = dataLogins.Rows.Count;
                        if (count == 1)
                        {
                            DataRow dr     = dataLogins.Rows[0];
                            string  dbname = (string)dr["name"];
                            username = dbname.Trim();
                        }
                        else if (count == 0)
                        {
                            return(string.Format("Error: cannot find an account with that email"));
                        }
                        else
                        {
                            return(string.Format("Error: {0} users found with that email", dataLogins.Rows.Count));
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error = ex.Message;
                    return("DB error: " + ex.Message);
                }

                // create and send an email
                try
                {
                    // create a code based on data
                    string emailCode = Logdata.GetHash(username + username);

                    // string URLstr = string.Format("https://quilkin.co.uk/tccrides?pwuser={0}&regcode={1}", username, emailCode);
                    string URLstr = string.Format(Connections.serviceURL + "?pwuser={0}&regcode={1}", username, emailCode);

                    EmailConnection ec      = new EmailConnection();
                    MailAddress     from    = new MailAddress("*****@*****.**");
                    MailMessage     message = new MailMessage(from, emailAddr)
                    {
                        Subject = "TCC rides forgotten password",
                        Body    = string.Format("Please click {0}  to reset your password or other details.\n\rFor security, this link will expire in 15 minutes!", URLstr)
                    };

                    try
                    {
                        SmtpClient client = new System.Net.Mail.SmtpClient(ec.Server)
                        {
                            Credentials = new System.Net.NetworkCredential(ec.User, ec.PW)
                        };
                        client.Send(message);

                        // save the time this message was delivered

                        query = string.Format("update logins set messagetime = '{0}' where email = '{1}'", Logdata.DBTimeString(DateTime.Now), email);

                        try
                        {
                            var cmd = new MySqlCommand(query, gpxConnection.Connection);
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex2)
                        {
                            result    = "There is a database error, please try again:" + ex2.Message;
                            log.Error = ex2.Message;
                        }
                        result = "OK, now please wait for an email and click the link to set a new password";
                    }
                    catch (Exception ex)
                    {
                        result    = "Sorry, there is an error with the email service: " + ex.Message;
                        log.Error = ex.Message;
                    }
                }
                catch (Exception ex2)
                {
                    return("Error: " + ex2.Message);
                }
                finally
                {
                    log.Result = result;
                    log.Save(gpxConnection);
                    gpxConnection.Close();
                }
                return(result);
            }
            else
            {
                return(DBConnection.ErrStr);
            }
        }
示例#3
0
        public string Signup(Login login)
        {
            LogEntry log = new LogEntry(GetIP(), "Signup", new JavaScriptSerializer().Serialize(login));


            System.Net.Mail.MailAddress emailAddr;
            string result = "OK, now please enter code from email and resubmit details";

            try
            {
                emailAddr = new System.Net.Mail.MailAddress(login.Email);
                // Valid address
            }
            catch
            {
                return("This email address appears to be invalid");
            }
            if (login.PW.Length < 4 || login.PW.Length > 10)
            {
                return("Password must be between 4 and 10 characters");
            }

            string query = "SELECT Id, name, pw, email FROM logins";

            if (gpxConnection.IsConnect())
            {
                if (login.Code == 0)
                // not yet confirmed the signup
                {
                    using (MySqlDataAdapter loginAdapter = new MySqlDataAdapter(query, gpxConnection.Connection))
                    {
                        dataLogins = new DataTable();
                        loginAdapter.Fill(dataLogins);

                        int length = dataLogins.Rows.Count;
                        for (int row = 0; row < length; row++)
                        {
                            DataRow dr     = dataLogins.Rows[row];
                            string  dbname = (string)dr["name"];
                            dbname = dbname.Trim();
                            string dbpw = (string)dr["pw"];
                            dbpw = dbpw.Trim();
                            if (dbname == login.Name)
                            {
                                result = "Sorry, this username has already been taken";
                                break;
                            }
                        }
                    }
                }
                else if (login.Code == login.CalcCode())
                {
                    query = string.Format("insert into logins (name, pw, email) values ('{0}','{1}','{2}',)\n\r",
                                          login.Name, login.PW, login.Email);
                    try
                    {
                        var cmd = new MySqlCommand(query, gpxConnection.Connection);
                        cmd.ExecuteNonQuery();
                        result = "Thank you, you have now registered";
                    }
                    catch
                    {
                        result = "There is a database error, please try again";
                    }
                }
                else
                {
                    result = "There is an error with the code number, please try again";
                }



                if (login.Code == 0)
                // not yet confirmed the signup
                {
                    // create a code based on data
                    login.Code = login.CalcCode();

                    EmailConnection             email   = new EmailConnection();
                    System.Net.Mail.MailAddress from    = new System.Net.Mail.MailAddress("*****@*****.**");
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, emailAddr);
                    message.Subject = "BLE log signup";
                    message.Body    = string.Format("Please enter the code {0} into the signup page to complete your registration", login.Code);

                    try
                    {
                        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(email.Server);
                        //client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                        client.Credentials = new System.Net.NetworkCredential(email.User, email.PW);
                        client.Send(message);
                    }
                    catch (Exception ex)
                    {
                        result = "Sorry, there is an error with the email service: " + ex.Message;
                    }
                }
                log.Result = result;
                log.Save(gpxConnection);

                gpxConnection.Close();

                return(result);
            }
            else
            {
                return("No DB Connecton");
            }
        }