public static bool LogIn(string uun, byte[] passwordHash) { if (TestCredentials(uun, passwordHash)) { LogInAs(uun); using (var model = new Eumm.Model()) { model.Members.Attach(CurrentMember); CurrentMember.LastLogin = DateTime.Now; model.SaveChangesAsync(); } return true; } return false; }
public static IEnumerable<DbEntityValidationResult> Register(Member member, bool logIn = true) { if (member.Salt == null) { member.Salt = new byte[Constants.SaltLength]; Constants.RNG.GetBytes(member.Salt); } if (member.Password == null || member.Password.Length != Constants.PasswordHashLength) { throw new Exception("Invalid password hash"); } if (member.JoinDate == null) member.JoinDate = DateTime.Now; if (string.IsNullOrWhiteSpace(member.Email) && member.UUN.StartsWith("s", true, null)) { member.Email = member.UUN + '@' + Constants.UniEmailHost; } // Prepend member's salt var hash = new byte[member.Salt.Length + Constants.PasswordHashLength]; Array.Copy(member.Salt, hash, Constants.SaltLength); Array.Copy(member.Password, 0, hash, Constants.SaltLength, Constants.PasswordHashLength); member.Password = hash; using (var model = new Eumm.Model()) { model.Database.Delete(); model.Members.Add(member); try { model.SaveChanges(); } catch (DbEntityValidationException) { return model.GetValidationErrors(); } } if (logIn) LogInAs(member.UUN); return null; }
internal static bool TestCredentials(string uun, byte[] passwordHash) { // Get existing member entry Member member = null; using (var model = new Eumm.Model()) { member = model.Members.Find(uun); } if (member == null) return false; // Prepend member's salt var hash = new byte[member.Salt.Length + passwordHash.Length]; Array.Copy(member.Salt, hash, member.Salt.Length); Array.Copy(passwordHash, 0, hash, member.Salt.Length, passwordHash.Length); // Compare hashes if (member.Password.Length != hash.Length) return false; for (int i = 0; i < hash.Length; i++) { if (hash[i] != member.Password[i]) return false; } return true; }