protected void btnSave_Click(object sender, EventArgs e) { try { // Validate input value if (string.IsNullOrEmpty(txtUserName.Value.Trim()) || string.IsNullOrEmpty(txtEmail.Value.Trim()) ) { txtMess.ForeColor = Color.Red; txtMess.Text = "Missing input data!"; return; } // Account logged = (Account)Session["LoggedAccount"]; int Id = Int32.Parse(AccountID.Value); LPS.Account obj = db.Accounts.FirstOrDefault(x => x.AccountId == Id); if (obj != null) { if (radioAdmin.Checked) { obj.AccountType = Common.TypeAdmin; } else { obj.AccountType = Common.TypeUser; } obj.Active = true; //obj.CreateBy = logged.UserName ; obj.Email = txtEmail.Value.Trim(); obj.FullName = txtFullName.Value.Trim(); obj.ModifyDate = DateTime.Now; if (!string.IsNullOrEmpty(txtPassword.Value.Trim())) { if (txtPassword.Value.Trim().ToLower() == txtPasswordConfirm.Value.Trim().ToLower()) { obj.Password = haenClass.SHA256Hash(txtPassword.Value.Trim()); } else { txtMess.ForeColor = Color.Red; txtMess.Text = "Password confirm not match!"; return; } } db.SaveChanges(); LoadData(); txtMess.ForeColor = Color.Green; txtMess.Text = "The account with username " + obj.UserName + " was updated successful"; } } catch (Exception ex) { txtMess.ForeColor = Color.Red; txtMess.Text = ex.Message; } }
protected void btnSave_Click(object sender, EventArgs e) { try { // Validate input value if (string.IsNullOrEmpty(txtUserName.Value.Trim()) || string.IsNullOrEmpty(txtPassword.Value.Trim()) || txtPassword.Value != txtPasswordConfirm.Value || string.IsNullOrEmpty(txtEmail.Value.Trim()) ) { txtMess.ForeColor = Color.Red; txtMess.Text = "Missing input data!"; return; } // Check username if exist if (db.Accounts.FirstOrDefault(x => x.UserName == txtUserName.Value.Trim()) != null) { txtMess.ForeColor = Color.Red; txtMess.Text = "This Username was existed"; return; } LPS.Account obj = new LPS.Account(); if (radioAdmin.Checked) { obj.AccountType = Common.TypeAdmin; } else { obj.AccountType = Common.TypeUser; } obj.Active = true; obj.CreateBy = logged.UserName; obj.CreateDate = DateTime.Now; obj.Email = txtEmail.Value.Trim(); obj.FullName = txtFullName.Value.Trim(); obj.ModifyDate = DateTime.Now; obj.Password = haenClass.SHA256Hash(txtPassword.Value.Trim()); obj.UserName = txtUserName.Value.Trim(); db.Accounts.Add(obj); db.SaveChanges(); LoadData(); txtMess.ForeColor = Color.Green; txtMess.Text = "The account with username " + obj.UserName + " was created successful"; } catch (Exception ex) { txtMess.ForeColor = Color.Red; txtMess.Text = ex.Message; return; } }
protected void Page_Load(object sender, EventArgs e) { txtError.Visible = false; txtError.Text = string.Empty; //Session.Abandon(); if (IsPostBack) { lafien_products_dbEntities db = new lafien_products_dbEntities(); HashEncryption hasEn = new HashEncryption(); string enPass = hasEn.SHA256Hash(txtPassword.Value); Account obj = db.Accounts.FirstOrDefault(x => x.UserName == txtUsername.Value && x.Password == enPass); if (obj != null) { HttpContext.Current.Session["LoggedAccount"] = obj; Response.Redirect("~/Pages/Default.aspx"); } else { txtError.Visible = true; txtError.Text = "Invalid Username or Password!"; } } }