public MembershipUser CreateUser(string username, string password, string email) { using (ApplicationServices db = new ApplicationServices()) { User user = new User(); user.Name = ""; user.Username = username; user.Email = email; user.PasswordSalt = CreateSalt(); user.Password = CreatePasswordHash(password, user.PasswordSalt); user.CreatedAt = DateTime.Now; user.LastModifiedAt = DateTime.Now; user.IsActivated = false; user.IsLockedOut = false; user.LastLockedOutAt = DateTime.Now; user.LastLoginAt = DateTime.Now; user.NewPasswordRequestedAt = DateTime.Now; user.NewEmailKey = GenerateKey(); db.Users.Add(user); db.SaveChanges(); SendActivationEmail(user); return GetUser(username); } }
public MembershipUser GetUser(string username) { using (ApplicationServices db = new ApplicationServices()) { var result = from u in db.Users where (u.Username == username) select u; if (result.Count() != 0) { var dbuser = result.FirstOrDefault(); string _username = dbuser.Username; int _providerUserKey = dbuser.ID; string _email = dbuser.Email; string _passwordQuestion = ""; bool _isApproved = dbuser.IsActivated; bool _isLockedOut = dbuser.IsLockedOut; DateTime _creationDate = dbuser.CreatedAt; DateTime _lastLoginDate = dbuser.LastLoginAt; DateTime _lastActivityDate = DateTime.Now; DateTime _lastPasswordChangedDate = DateTime.Now; DateTime _lastLockedOutDate = dbuser.LastLockedOutAt; MembershipUser user = new MembershipUser("CustomMembershipProvider", _username, _providerUserKey, _email, _passwordQuestion, "", _isApproved, _isLockedOut, _creationDate, _lastLoginDate, _lastActivityDate, _lastPasswordChangedDate, _lastLockedOutDate); return user; } else { return null; } } }
public string GetUserNameByEmail(string email) { using (ApplicationServices db = new ApplicationServices()) { var result = from u in db.Users where (u.Email == email) select u; if (result.Count() != 0) { var dbuser = result.FirstOrDefault(); return dbuser.Username; } else { return ""; } } }
public bool ActivateUser(string username, string key) { using (ApplicationServices db = new ApplicationServices()) { var result = from u in db.Users where (u.Username == username) select u; if (result.Count() != 0) { var dbuser = result.First(); if (dbuser.NewEmailKey == key) { dbuser.IsActivated = true; dbuser.LastModifiedAt = DateTime.Now; dbuser.NewEmailKey = null; db.SaveChanges(); return true; } else { return false; } } else { return false; } } }
public bool ValidateUser(string username, string password) { using (ApplicationServices db = new ApplicationServices()) { var result = from u in db.Users where (u.Username == username) select u; if (result.Count() != 0) { var dbuser = result.First(); if (dbuser.Password == CreatePasswordHash(password, dbuser.PasswordSalt) && dbuser.IsActivated == true) return true; else return false; } else { return false; } } }