protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request["ApiKey"] != null) { System.Guid guid = new Guid(Request["ApiKey"]); ThermostatMonitorLib.Location location = ThermostatMonitorLib.Location.LoadLocation(guid); if (location != null) { ThermostatMonitorLib.User user = ThermostatMonitorLib.User.LoadUser(location.UserId); if (user != null) { AppUser.Current.Login(user); Response.Redirect("/cp/"); } } } else if (Request["action"] == "logout") { AppUser.Current.Logout(); Response.Redirect("/"); } } }
protected void LoginButton_Click(object sender, EventArgs e) { ThermostatMonitorLib.User user = null; List <string> errors = new List <string>(); if (EmailText.Text == "") { errors.Add("Email is blank."); } if (PasswordText.Text == "") { errors.Add("Password is blank."); } if (errors.Count == 0) { user = ThermostatMonitorLib.User.LoadUser(EmailText.Text, PasswordText.Text); if (user == null) { errors.Add("Invalid email or password"); } } if (errors.Count != 0) { ErrorLit.Text = "<tr><td colspan=\"2\"><div class=\"error\">" + String.Join(" ", errors.ToArray()) + "</div></td></tr>"; } else { AppUser.Current.Login(user); Response.Redirect("/cp/"); } }
protected void Page_Load(object sender, EventArgs e) { string email = Request["Email"]; string password = Request["Password"]; ThermostatMonitorLib.User user = ThermostatMonitorLib.User.LoadUser(email, password); if (user != null) { AppUser.Current.Login(user); } }
private bool Validate() { List <string> errors = new List <string>(); ThermostatMonitorLib.User user = ThermostatMonitorLib.User.LoadUser(EmailText.Text); if (!AgreeCheck.Checked) { errors.Add("You must agree to the terms of use."); } if (!EmailText.Text.Contains("@")) { errors.Add("Invalid email address."); } if (ZipCodeText.Text.Length != 5) { errors.Add("Please enter a five digit zip code."); } if (PriceText.Text == "") { errors.Add("Plese list a price. You can change it at any time."); } double price; if (!Double.TryParse(PriceText.Text, out price)) { errors.Add("Please list electricity price as cents per kilowatt hour."); } double heatPrice; if (!Double.TryParse(HeatPriceText.Text.Replace("$", "").Replace(",", ""), out heatPrice)) { errors.Add("Please list heat price as dollars per Dekatherm."); } if (user != null) { errors.Add("An account is already registered with this email address."); } if (Password2Text.Text != PasswordText.Text) { errors.Add("Passwords do not match."); } if (errors.Count == 0) { ErrorLit.Text = ""; } else { ErrorLit.Text = "<div class=\"error\">" + String.Join(" ", errors.ToArray()) + "</div>"; } return(errors.Count == 0); }
protected void SaveButton_Click(object sender, EventArgs e) { if (Validate()) { ThermostatMonitorLib.User user = ThermostatMonitorLib.User.LoadUser(AppUser.Current.UserData.Id); if (PasswordText.Text.Length > 0) { user.Password = ThermostatMonitorLib.Utils.HashPassword(PasswordText.Text); } ThermostatMonitorLib.User.SaveUser(user); AppUser.Current.UserData = user; int systemTimezone = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Timezone"]); Response.Redirect("/cp/"); } }
protected void ResetButton_Click(object sender, EventArgs e) { ThermostatMonitorLib.User user = ThermostatMonitorLib.User.LoadUser(EmailText.Text); if (user == null) { ErrorLit.Text = "<div class=\"error\">Could not find a user with this email address</div>"; return; } else { string password = GeneratePassword(); user.Password = ThermostatMonitorLib.Utils.HashPassword(password); ThermostatMonitorLib.User.SaveUser(user); SendEmail(user.EmailAddress, password); ErrorLit.Text = "<div>Password reset email has been sent.</div>"; } }
protected void Page_Init(object sender, EventArgs e) { if (!AppUser.Current.IsAuthenticated) { string authCode = Request["authCode"]; if (authCode != null && authCode != "") { ThermostatMonitorLib.User user = ThermostatMonitorLib.User.LoadByAuthCode(authCode); if (user != null) { AppUser.Current.Login(user); } } if (!AppUser.Current.IsAuthenticated) { Response.Redirect("/cp/login.aspx"); } } }
protected void RegisterButton_Click(object sender, EventArgs e) { if (Validate()) { ThermostatMonitorLib.User user = new ThermostatMonitorLib.User(); user.EmailAddress = EmailText.Text; user.Password = ThermostatMonitorLib.Utils.HashPassword(PasswordText.Text); user.AuthCode = Guid.NewGuid().ToString(); ThermostatMonitorLib.User.SaveUser(user); ThermostatMonitorLib.Location location = new ThermostatMonitorLib.Location(); location.ApiKey = Guid.NewGuid().ToString(); location.ElectricityPrice = Convert.ToDouble(PriceText.Text); location.HeatFuelPrice = Convert.ToDouble(HeatPriceText.Text.Replace("$", "").Replace(",", "")); location.Name = "Home"; location.UserId = user.Id; location.ZipCode = ZipCodeText.Text; location.ShareData = ShareCheck.Checked; location.Timezone = Convert.ToInt32(TimezoneList.SelectedValue); location.DaylightSavings = DaylightSavings.Checked; //location.DaylightSavings = DaylightSavings.Checked; location.OpenWeatherCityId = ThermostatMonitorLib.Weather.GetCityId(location.ZipCode); ThermostatMonitorLib.Location.SaveLocation(location); //ThermostatMonitorLib.UserSetting setting = new ThermostatMonitorLib.UserSetting(); //setting.ZipCode = ZipCodeText.Text; //setting.UserId = user.Id; //setting.FilterChangeDate = DateTime.Today.AddDays(90); //ThermostatMonitorLib.UserSetting.SaveUserSetting(setting); AppUser.Current.Login(user); Response.Redirect("/cp/"); } }
protected void RegisterButton_Click(object sender, EventArgs e) { if (Validate()) { ThermostatMonitorLib.User user = new ThermostatMonitorLib.User(); user.EmailAddress = EmailText.Text; user.Password = ThermostatMonitorLib.Utils.HashPassword(PasswordText.Text); user.AuthCode = Guid.NewGuid().ToString(); ThermostatMonitorLib.User.SaveUser(user); ThermostatMonitorLib.Location location = new ThermostatMonitorLib.Location(); location.ApiKey = Guid.NewGuid(); location.ElectricityPrice = Convert.ToDouble(PriceText.Text); location.HeatFuelPrice = Convert.ToDouble(HeatPriceText.Text.Replace("$", "").Replace(",", "")); location.Name = "Home"; location.UserId = user.Id; location.ZipCode = ZipCodeText.Text; location.ShareData = ShareCheck.Checked; location.Timezone = Convert.ToInt32(TimezoneList.SelectedValue); location.DaylightSavings = DaylightSavings.Checked; //location.DaylightSavings = DaylightSavings.Checked; location.OpenWeatherCityId = ThermostatMonitorLib.Weather.GetCityId(location.ZipCode); ThermostatMonitorLib.Location.SaveLocation(location); //ThermostatMonitorLib.UserSetting setting = new ThermostatMonitorLib.UserSetting(); //setting.ZipCode = ZipCodeText.Text; //setting.UserId = user.Id; //setting.FilterChangeDate = DateTime.Today.AddDays(90); //ThermostatMonitorLib.UserSetting.SaveUserSetting(setting); AppUser.Current.Login(user); Response.Redirect("/cp/"); } }
public void Logout() { UserData = null; IsAuthenticated = false; }
public void Login(ThermostatMonitorLib.User user) { UserData = user; IsAuthenticated = true; }
internal static User GetUser(DataRow row) { User result = new User(); if (row.Table.Columns.Contains("id")) { if (Convert.IsDBNull(row["id"])) { result._isIdNull = true; } else { result._id = Convert.ToInt32(row["id"]); result._isIdNull = false; } } if (row.Table.Columns.Contains("email_address")) { if (Convert.IsDBNull(row["email_address"])) { result._isEmailAddressNull = true; } else { result._emailAddress = Convert.ToString(row["email_address"]); result._isEmailAddressNull = false; } } if (row.Table.Columns.Contains("password")) { if (Convert.IsDBNull(row["password"])) { result._isPasswordNull = true; } else { result._password = Convert.ToString(row["password"]); result._isPasswordNull = false; } } if (row.Table.Columns.Contains("auth_code")) { if (Convert.IsDBNull(row["auth_code"])) { result._isAuthCodeNull = true; } else { result._authCode = Convert.ToString(row["auth_code"]); result._isAuthCodeNull = false; } } return result; }
public static int SaveUser(User user) { int result = 0; MySqlCommand cmd = new MySqlCommand("users_save", ThermostatMonitorLib.Global.MySqlConnection); cmd.CommandType = CommandType.StoredProcedure; if (user._isIdNull) { cmd.Parameters.AddWithValue("@id", System.DBNull.Value); } else { cmd.Parameters.AddWithValue("@id", user._id); } if (user._isEmailAddressNull) { cmd.Parameters.AddWithValue("@email_address", System.DBNull.Value); } else { cmd.Parameters.AddWithValue("@email_address", user._emailAddress); } if (user._isPasswordNull) { cmd.Parameters.AddWithValue("@password", System.DBNull.Value); } else { cmd.Parameters.AddWithValue("@password", user._password); } if (user._isAuthCodeNull) { cmd.Parameters.AddWithValue("@auth_code", System.DBNull.Value); } else { cmd.Parameters.AddWithValue("@auth_code", user._authCode); } cmd.Connection.Open(); try { result = Convert.ToInt32(cmd.ExecuteScalar()); } finally { cmd.Connection.Close(); } user.Id = result; return result; }