public LoginStatus ValidateUser(DTOAuthentication Authentication) { // LoginStatus to return LoginStatus objLoginStatus = new LoginStatus(); objLoginStatus.isLoggedIn = false; // Get values passed var paramUserName = Authentication.userName; var paramPassword = Authentication.password; if ((paramUserName != null) && (paramPassword != null)) { var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(GetConnectionString()); // Only check the legacy User password if user is not in the main table if (_userManager.Users.Where(x => x.UserName == paramUserName).FirstOrDefault() == null) { using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { // First check the legacy User table var objAdefHelpDeskUser = (from AdefHelpDeskUsers in context.AdefHelpDeskUsers where AdefHelpDeskUsers.Username == paramUserName where AdefHelpDeskUsers.Password != "" select AdefHelpDeskUsers).FirstOrDefault(); if (objAdefHelpDeskUser != null) { // User is in the Legacy table and the password is not null // Check their password to see if this account can be migrated if (objAdefHelpDeskUser.Password == ComputeHash.GetSwcMD5(paramUserName.Trim().ToLower() + paramPassword.Trim())) { // Return that this account can be migrated objLoginStatus.status = "Migrate"; return(objLoginStatus); } } } } // Check to see if the user needs to Verify their account using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { var objAdefHelpDeskUser = (from AdefHelpDeskUsers in context.AdefHelpDeskUsers where AdefHelpDeskUsers.Username == paramUserName select AdefHelpDeskUsers).FirstOrDefault(); if (objAdefHelpDeskUser != null) { if (objAdefHelpDeskUser.VerificationCode != null) { objLoginStatus.status = "Verify"; return(objLoginStatus); } } } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = _signInManager.PasswordSignInAsync( paramUserName, paramPassword, false, lockoutOnFailure: false).Result; if (result.Succeeded) { objLoginStatus.status = "Success"; objLoginStatus.isLoggedIn = true; return(objLoginStatus); } if (result.RequiresTwoFactor) { objLoginStatus.status = "RequiresVerification"; return(objLoginStatus); } if (result.IsLockedOut) { objLoginStatus.status = "IsLockedOut"; return(objLoginStatus); } } objLoginStatus.status = "Authentication Failure"; return(objLoginStatus); }
public IActionResult Index([FromBody] DTOMigration Migration) { // LoginStatus to return LoginStatus objLoginStatus = new LoginStatus(); objLoginStatus.isLoggedIn = false; if ((Migration.userName != null) && (Migration.password != null) && (Migration.passwordNew != null)) { // Get values passed var paramUserName = Migration.userName; var paramPassword = ComputeHash.GetSwcMD5(paramUserName.Trim().ToLower() + Migration.password.Trim()); var paramPasswordNew = Migration.passwordNew; var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(GetConnectionString()); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { // Must be in legacy User table var objAdefHelpDeskUser = (from AdefHelpDeskUsers in context.AdefHelpDeskUsers where AdefHelpDeskUsers.Username == paramUserName where AdefHelpDeskUsers.Password == paramPassword select AdefHelpDeskUsers).FirstOrDefault(); if (objAdefHelpDeskUser != null) { // Email Validation **************************** if (objAdefHelpDeskUser.Email == null) { objLoginStatus.status = "The Email for this account is not valid. It cannot be migrated."; objLoginStatus.isLoggedIn = false; return(Ok(objLoginStatus)); } EmailValidation objEmailValidation = new EmailValidation(); if (!objEmailValidation.IsValidEmail(objAdefHelpDeskUser.Email)) { objLoginStatus.status = "The Email for this account is not valid. It cannot be migrated."; objLoginStatus.isLoggedIn = false; return(Ok(objLoginStatus)); } // Migrate Account var user = new ApplicationUser { UserName = paramUserName, Email = objAdefHelpDeskUser.Email }; var result = _userManager.CreateAsync(user, paramPasswordNew).Result; if (result.Succeeded) { // Sign the User in var SignInResult = _signInManager.PasswordSignInAsync( paramUserName, paramPasswordNew, false, lockoutOnFailure: false).Result; if (!SignInResult.Succeeded) { // Return the error objLoginStatus.status = $"Could not sign user {paramUserName} in."; objLoginStatus.isLoggedIn = false; return(Ok(objLoginStatus)); } else { try { // Everything worked // Update the users password in the legacy table objAdefHelpDeskUser.Password = ComputeHash.GetSwcMD5(paramUserName.Trim().ToLower() + paramPasswordNew.Trim()); context.SaveChanges(); } catch { // Do nothing if this does not work // This password is only needed if connecting from the older // Non Angular version of ADefHelpDesk } // Success objLoginStatus.status = $"Logged {paramUserName} in."; objLoginStatus.isLoggedIn = true; return(Ok(objLoginStatus)); } } else { // Return the errors from the Memberhip API Creation string strErrors = ""; foreach (var Error in result.Errors) { strErrors = strErrors + "\n" + Error.Description; } // Return the error objLoginStatus.status = strErrors; objLoginStatus.isLoggedIn = false; return(Ok(objLoginStatus)); } } else { objLoginStatus.status = "Orginal password does not match."; return(Ok(objLoginStatus)); } } } objLoginStatus.status = "Authentication Failure"; return(Ok(objLoginStatus)); }
public IActionResult Index([FromBody] DTOAuthentication Authentication) { // LoginStatus to return LoginStatus objLoginStatus = new LoginStatus(); objLoginStatus.isLoggedIn = false; // Get values passed var paramUserName = Authentication.userName; var paramPassword = Authentication.password; if ((paramUserName != null) && (paramPassword != null)) { // First log the user out if (this.User.Identity.IsAuthenticated) { // Log user out _signInManager.SignOutAsync().Wait(); } var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(GetConnectionString()); try { // Only check the legacy User password if user is not in the main table if (_userManager.Users.Where(x => x.UserName == paramUserName).FirstOrDefault() == null) { using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { // First check the legacy User table var objAdefHelpDeskUser = (from AdefHelpDeskUsers in context.AdefHelpDeskUsers where AdefHelpDeskUsers.Username == paramUserName where AdefHelpDeskUsers.Password != "" select AdefHelpDeskUsers).FirstOrDefault(); if (objAdefHelpDeskUser != null) { // User is in the Legacy table and the password is not null // Check their password to see if this account can be migrated if (objAdefHelpDeskUser.Password == ComputeHash.GetSwcMD5(paramUserName.Trim().ToLower() + paramPassword.Trim())) { // Return that this account can be migrated objLoginStatus.status = "Migrate"; return(Ok(objLoginStatus)); } } } } } catch { // There may have been an error because this is an upgrade from a version // of Adefhelpdesk before the AspNetUsers tables existed using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { // Check the legacy User table var objAdefHelpDeskUser = (from AdefHelpDeskUsers in context.AdefHelpDeskUsers where AdefHelpDeskUsers.Username == paramUserName where AdefHelpDeskUsers.Password != "" select AdefHelpDeskUsers).FirstOrDefault(); if (objAdefHelpDeskUser != null) { // User is in the Legacy table and the password is not null // Check their password if (objAdefHelpDeskUser.Password == ComputeHash.GetSwcMD5(paramUserName.Trim().ToLower() + paramPassword.Trim())) { // This database must be upgraded to ass the AspNetUseers table (for anything else to work) InstallWizardController.RunUpdateScripts("00.00.00", _hostEnvironment, GetConnectionString()); // Return that this account can be migrated objLoginStatus.status = "Migrate"; return(Ok(objLoginStatus)); } else { objLoginStatus.status = "Error: Account needs to be migrated, but account cannot be migrated because the password is incorrect"; return(Ok(objLoginStatus)); } } } } // Check to see if the user needs to Verify their account using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { var objAdefHelpDeskUser = (from AdefHelpDeskUsers in context.AdefHelpDeskUsers where AdefHelpDeskUsers.Username == paramUserName select AdefHelpDeskUsers).FirstOrDefault(); if (objAdefHelpDeskUser != null) { if (objAdefHelpDeskUser.VerificationCode != null) { objLoginStatus.status = "Verify"; return(Ok(objLoginStatus)); } } } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = _signInManager.PasswordSignInAsync( paramUserName, paramPassword, false, lockoutOnFailure: false).Result; if (result.Succeeded) { objLoginStatus.status = "Success"; objLoginStatus.isLoggedIn = true; return(Ok(objLoginStatus)); } if (result.RequiresTwoFactor) { objLoginStatus.status = "RequiresVerification"; return(Ok(objLoginStatus)); } if (result.IsLockedOut) { objLoginStatus.status = "IsLockedOut"; return(Ok(objLoginStatus)); } } objLoginStatus.status = "Authentication Failure"; return(Ok(objLoginStatus)); }