public static Boolean IsAuthenticated(String username, String password)
    {
        LDAPManager manager         = new LDAPManager();
        bool        isAuthenticated = manager.IsAuthenticated(username, password, ConfigurationManager.AppSettings["DomainName"],
                                                              ConfigurationManager.AppSettings["ActiveDirectoryPath"]);

        return(isAuthenticated);
    }
    public static HashSet <User> GetActiveDirectoryUsers()
    {
        HashSet <User> adUsers = new HashSet <User>();
        LDAPManager    manager = new LDAPManager();

        adUsers = manager.GetActiveDirectoryUsers(ConfigurationManager.AppSettings["ActiveDirectoryGroups"],
                                                  ConfigurationManager.AppSettings["ActiveDirectoryPath"]);

        return(adUsers);
    }
        public IActionResult Login(UserLoginDataModel userLogin)
        {
            // New attempt at logging in, means old login attempt errors are irrelevant.
            HttpContext.Session.SetString("loginError", "");

            if ((userLogin.UNILogin != "" && userLogin.UNILogin != null) && (userLogin.Password != "" && userLogin.Password != null))
            {
                // Verify and acquire the user's relevant groups for access and any errors encountered in this endeavour (i.e. "Unable to establish connection")
                //List<string> reponses = LDAPManager.GetAccessResponses(userLogin.UNILogin, userLogin.Password);
                List <string> responses = LDAPManager.TestLogin(userLogin.UNILogin, userLogin.Password);

                // Set Session data accordingly.
                if (responses.Count > 0)
                {
                    HttpContext.Session.SetString("uniLogin", userLogin.UNILogin);

                    // 0 (Impossible) = No access to anything. 1 = Teacher, access to frontend.
                    // 2 = SKP Student, access to most backend. 3 = SKP Teacher, full backend access.
                    int accessLevel = 0;
                    foreach (string response in responses)
                    {
                        if (response == "ZBC-Ri-skpElev")
                        {
                            accessLevel += 2;
                        }
                        else if (response == "ZBC-RIAH-Ansatte")
                        {
                            accessLevel += 1;
                        }
                        else if (response.Contains("FEJL: "))
                        {
                            HttpContext.Session.SetString("loginError", response.Substring(6));
                        }
                    }

                    HttpContext.Session.SetInt32("accessLevel", accessLevel);
                }

                // If the user is not a member of any groups, and there is no existing explanation as to why (i.e. error saying username or password incorrect)
                // -Then it means that the user is neither a SKP student or a ZBC Employee.
                if (responses.Count == 0 && HttpContext.Session.GetString("loginError") == "")
                {
                    HttpContext.Session.SetString("loginError", "Adgang Nægtet: Du har ikke medlemskab af relevante grupper");
                }
            }
            else
            {
                HttpContext.Session.SetString("loginError", "Udfyld uniLogin og kodeord. UniLogin er din ZBC email uden \"@zbc.dk\".");
            }

            return(RelocateUser());
        }
        public UserViewModel AuthenticateUser(string username, string password, bool useLDAP)
        {
            if (useLDAP)
            {
                var ldap   = new LDAPManager();
                var result = ldap.AuthenticateLDAP(username, password);
                if (result)
                {
                    CurrentUser = userRepository.GetUserByUsername(username);
                }
            }
            else
            {
                CurrentUser = userRepository.ValidateUsernameAndPassword(username, password);
            }


            return(CurrentUser);
        }
        public void ImportLDAPUser()
        {
            var s = new LDAPManager().SyncOrganizationalUnits();


            foreach (LDAPGroupInformation l in s)
            {
                int departmentId = 0;
                var existingDept = Singleton.Instance.CompanyDepartmentModel.GetDepartment(l.Name);
                if (existingDept != null)
                {
                    departmentId = existingDept.Id;
                }
                else
                {
                    departmentId = Singleton.Instance.CompanyDepartmentModel.CreateDepartment(l.Name);
                }

                if (departmentId > 0)
                {
                    foreach (LDAPUserInformation ui in l.Members)
                    {
                        var existing = Singleton.Instance.UserModel.GetUsersByUsername(ui.Username);
                        var userType = ui.IsAdministrator ? UserModel.UserType.Admin : UserModel.UserType.User;
                        if (existing == null)
                        {
                            Singleton.Instance.UserModel.CreateNewUser(ui.Username, "", userType, ui.Firstname, ui.Lastname, departmentId, ui.Gender == "M", ui.Disabled);
                        }
                        else
                        {
                            Singleton.Instance.UserModel.UpdateUser(existing.Id, ui.Firstname, ui.Lastname, departmentId, userType, "", ui.Disabled);
                        }
                    }
                }
            }
        }