示例#1
0
        public string Signup(Login 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";
            try
            {
                ttConnection = new SqlConnection(connection);
                ttConnection.Open();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                return ex.Message;
            }
            if (login.Code == 0)
            // not yet confirmed the signup
            {
                using (SqlDataAdapter loginAdapter = new SqlDataAdapter(query, ttConnection))
                {
                    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, clubID) values ('{0}','{1}','{2}','{3}')\n\r",
                    login.Name, login.PW, login.Email, 0);
                using (System.Data.SqlClient.SqlCommand command = new SqlCommand(query, ttConnection))
                {
                    command.ExecuteNonQuery();
                }
                result = "Thank you, you have now registered";
            }
            else
            {
                result = "There is an error with the code number, please try again";
            }
            ttConnection.Close();

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

                System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("*****@*****.**");
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, emailAddr);
                message.Subject = "TimeTrials 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(smtpserver);
                    //client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                    client.Credentials = new System.Net.NetworkCredential(smtpUserName, smtpPassword);
                    client.Send(message);
                }
                catch (Exception ex)
                {
                    result = "Sorry, there is an error with the email service: " + ex.Message;
                }
            }
            return  result ;
        }
示例#2
0
        public int Login(Login login)
        {
            string query = "SELECT Id, name, pw, email, role FROM logins";
            try
            {
                ttConnection = new SqlConnection(connection);
                ttConnection.Open();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
               // return ex.Message;
            }
            int userRole = 0;
            using (SqlDataAdapter loginAdapter = new SqlDataAdapter(query, ttConnection))
            {
                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 && dbpw == login.PW)
                    {
                        userRole = (int)dr["role"];
                        break;
                    }
                }
            }
            ttConnection.Close();
            return userRole;
        }