public LoginViewModel Insert(LoginViewModel loginViewModel) { try { var user = _db.ONNET_USER.FirstOrDefault(u => u.Username == loginViewModel.Username && u.Password == loginViewModel.Password); if (user == null) { var newuser = new ONNET_USER { Username = loginViewModel.Username, //Password = Encrypt(new LoginViewModel //{ Password = loginViewModel.Password, //}), Email = null, Mobile = null, Department = null, Title = null, Status = null, RoleId = 3 }; using (MaxisEntities _db = new MaxisEntities()) { _db.ONNET_USER.Add(newuser); _db.SaveChanges(); } var role = (from ep in _db.ONNET_USER join e in _db.ONNET_USERROLE on ep.RoleId equals e.RoleId where loginViewModel.Username == ep.Username select new LoginViewModel() { Roles = e.RoleName, RoleId = e.RoleId }).Single(); return(role); } else { var role = (from ep in _db.ONNET_USER join e in _db.ONNET_USERROLE on ep.RoleId equals e.RoleId where loginViewModel.Username == ep.Username select new LoginViewModel() { Roles = e.RoleName, RoleId = e.RoleId }).Single(); return(role); } } catch (Exception) { throw; } }
public UserDetailsViewModel ValidateUser(LoginViewModel loginViewModel, bool ldap) { try { if (ldap) { var user = _db.ONNET_USER.FirstOrDefault(u => u.Username == loginViewModel.Username); if (user != null) { return(GetRoles(loginViewModel).FirstOrDefault()); } var salt = CreateSalt(int.Parse(WebConfigurationManager.AppSettings["salt"])); var encyptval = Encrypt(loginViewModel.Password, salt); var newuser = new ONNET_USER { Username = loginViewModel.Username, Password = salt, RoleId = (long)Enum.Roles.Normal, PasswordHash = encyptval }; _db.ONNET_USER.Add(newuser); _db.SaveChanges(); return(GetRoles(loginViewModel).FirstOrDefault()); } else { var user = _db.ONNET_USER.FirstOrDefault(u => u.Username == loginViewModel.Username); if (user != null) { var generatedSalt = GetSalt(loginViewModel.Username); var hashedPassword = Encrypt(loginViewModel.Password, generatedSalt); var verifyUser = _db.ONNET_USER.FirstOrDefault( u => u.Username == loginViewModel.Username && u.PasswordHash == hashedPassword); return(verifyUser != null?GetRoles(loginViewModel).FirstOrDefault() : new UserDetailsViewModel { ErrorStatus = "Password mismatch" }); } else { return(new UserDetailsViewModel { ErrorStatus = "User not found" }); } } } catch (Exception ex) { throw ex; } }