public UC_NormalUserOperation() { InitializeComponent(); dataentity = new Model.WPF_ProjectDBEntities(); var q = from u in dataentity.UserInformations where u.Username == UserViewModel.CurrentUser select u; this.DataContext = q.ToList(); }
//建立驗證使用者帳號密碼的方法。 public static LogonErrorTypes ValidateUser(string userName, string password) { if (userName == "") { return(LogonErrorTypes.LackOfUsername); } if (password == "") { return(LogonErrorTypes.LackOfPassword); } Model.WPF_ProjectDBEntities db = new Model.WPF_ProjectDBEntities(); //確認DataEntity中是否有帳號密碼相符的資訊。 foreach (Model.UserInformation u in db.UserInformations) { if (u.Username == userName) { byte[] bytesInputPassword = Encoding.Unicode.GetBytes(password + Encoding.Unicode.GetString(u.salt)); SHA256Managed Algorism = new SHA256Managed(); byte[] hashedInputPassword = Algorism.ComputeHash(bytesInputPassword); if (u.Password.SequenceEqual(hashedInputPassword)) { switch (u.UserType) { case "admin": return(LogonErrorTypes.ValidAdminUser); case "normal": return(LogonErrorTypes.ValidNormalUser); case "store": return(LogonErrorTypes.ValidStoreUser); default: break; } } else { return(LogonErrorTypes.InvalidUser); } } } return(LogonErrorTypes.UserNotExist); }
public static RegisterErrorTypes UpdateUser(string email, string firstname, string lastname, string mobile, Image profilePic) { //確認資訊填寫完整程度。 if (email == "") { return(RegisterErrorTypes.LackOfEmail); } Model.WPF_ProjectDBEntities db = new Model.WPF_ProjectDBEntities(); //Window mainWindow = Application.Current.MainWindow; //確認DataEntity中是否已存在相同email。 foreach (Model.UserInformation u in db.UserInformations) { if ((CurrentUser != u.Username) && (u.email == email)) { return(RegisterErrorTypes.DuplicateEmail); } } //將Image轉為byte[] MemoryStream ms = new MemoryStream(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create((BitmapSource)profilePic.Source)); encoder.Save(ms); byte[] imgData = ms.ToArray(); ms.Close(); //經多重檢查無誤後,將帳號資訊登錄於DataEntity中。 var q = from u in db.UserInformations where u.Username == CurrentUser select u; foreach (var user in q) { user.email = email; user.Firstname = firstname; user.Lastname = lastname; user.mobile = mobile; user.Photo = imgData; } db.SaveChanges(); return(RegisterErrorTypes.SuccessfulRegistration); }
//建立移除使用者的方法。 public static void DeleteUser(string currentUser) { Model.WPF_ProjectDBEntities db = new Model.WPF_ProjectDBEntities(); var q = from u in db.UserInformations where u.Username == currentUser select u; if (q != null) { db.UserInformations.RemoveRange(q); db.SaveChanges(); MessageBox.Show("成功移除帳號!"); } else { MessageBox.Show("移除失敗!"); } }
public Merchandise() { InitializeComponent(); animalShoppingDB = new Model.WPF_ProjectDBEntities(); Loading(); }
//建立登錄使用者的方法。 public static RegisterErrorTypes CreateUser(string userName, string password, string password2, string email, string firstname, string lastname, string mobile, Image profilePic) { //確認資訊填寫完整程度。 if (userName == "") { return(RegisterErrorTypes.LackOfUsername); } if (email == "") { return(RegisterErrorTypes.LackOfEmail); } if (password == "") { return(RegisterErrorTypes.LackOfPassword); } if (password != password2) { return(RegisterErrorTypes.InconsistentPassword); } Model.WPF_ProjectDBEntities db = new Model.WPF_ProjectDBEntities(); //確認DataEntity中是否已存在相同帳號。 foreach (Model.UserInformation u in db.UserInformations) { if (u.Username == userName) { return(RegisterErrorTypes.DuplicatedUsername); } if (u.email == email) { return(RegisterErrorTypes.DuplicateEmail); } } //用RNGCryptoServiceProvider產生隨機salt,加到cmd的參數中 RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] salt = new byte[32]; rng.GetBytes(salt); //將password加上salt後進行加密 byte[] bytesPassword = Encoding.Unicode.GetBytes(password + Encoding.Unicode.GetString(salt)); SHA256Managed Algorism = new SHA256Managed(); byte[] hashedPassword = Algorism.ComputeHash(bytesPassword); //將Image轉為byte[] MemoryStream ms = new MemoryStream(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create((BitmapSource)profilePic.Source)); encoder.Save(ms); byte[] imgData = ms.ToArray(); ms.Close(); //經多重檢查無誤後,將帳號資訊登錄於DataEntity中。 Model.UserInformation m = new Model.UserInformation { Username = userName, Password = hashedPassword, email = email, salt = salt, RegistrationDate = DateTime.Now, Firstname = firstname, Lastname = lastname, mobile = mobile, Photo = imgData, UserType = "normal" }; db.UserInformations.Add(m); db.SaveChanges(); return(RegisterErrorTypes.SuccessfulRegistration); }
public UC_NormalUserRegistration() { InitializeComponent(); data = new Model.WPF_ProjectDBEntities(); }