public IActionResult Index([FromBody] DTOAuthentication Register)
        {
            // Create Account ****************************
            try
            {
                var user = new ApplicationUser {
                    Email = Register.UserName
                };
                var result = _userManager.CreateAsync(user, Register.Password).Result;
                if (result.Succeeded)
                {
                    // Sign the User in
                    var SignInResult = _signInManager.PasswordSignInAsync(Register.UserName, Register.Password, false, lockoutOnFailure: false).Result;
                    if (!SignInResult.Succeeded)
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    return(BadRequest());
                }

                return(Ok(true));
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }
Beispiel #2
0
 public IActionResult Index([FromBody] DTOAuthentication Authentication)
 {
     // Get values passed
     if (Authentication.UserName != null && Authentication.Password != null)
     {
         var result = _signInManager.PasswordSignInAsync(Authentication.UserName, Authentication.Password, false, false).Result;
         if (result.Succeeded)
         {
             return(Ok(true));
         }
     }
     return(Ok(false));
 }
        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))
            {
                // 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));
        }
Beispiel #4
0
        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));
        }