Пример #1
0
        public bool Ping(int id, string Username)
        {
            try
            {
                bool pingResult = false;
                IQueryable <DomainSetting> domainSetting = _context.DomainSetting.Where(w => w.Id == id);

                var domain = domainSetting.Select(w => new DomainSetting
                {
                    UserName = w.UserName,
                    Server   = w.Server,
                    Title    = w.Title,
                    Password = w.Password
                }).FirstOrDefault();
                if (domain.Title != "کاربران سیستمی")
                {
                    string cipherText = "";
                    cipherText      = _encriptdescriptStringRepository.DecryptString(domain.Password);
                    domain.Password = cipherText;
                }
                List <DomainUser> lstADUsers     = new List <DomainUser>();
                DomainUser        objSurveyUsers = new DomainUser();
                string            dcString       = "";
                string            rootNode       = "";
                string[]          arrString;
                arrString = domain.Title.Split('.');
                if (arrString.Length == 1)
                {
                    dcString = "dc=" + domain.Title + "";
                    rootNode = arrString[0];
                }
                else
                {
                    for (int i = 0; i != arrString.Length; i++)
                    {
                        dcString += "dc=" + arrString[i].ToString() + ",";
                    }
                    if (arrString.Length == 3)
                    {
                        rootNode = arrString[1].ToString();
                    }
                    else if (arrString.Length == 2)
                    {
                        rootNode = arrString[0].ToString();
                    }
                    dcString = dcString.Substring(0, dcString.Length - 1);
                }


                string DomainPath = "LDAP://" + domain.Server + "/" + dcString;
                string ldapIp     = DomainPath.Split("//")[1];
                ldapIp = ldapIp.Split("/dc=")[0];
                int port = int.Parse(ldapIp.Length == 2 ? ldapIp : "389");
                pingResult = CustomPingServer.IsADSAlive(ldapIp, port);
                System.DirectoryServices.DirectoryEntry searchRoot = new System.DirectoryServices.DirectoryEntry(DomainPath);
                try
                {
                    object nativeObject = searchRoot.NativeObject;

                    searchRoot.Username = domain.UserName;
                    searchRoot.Password = domain.Password;
                    DirectorySearcher search = new DirectorySearcher(searchRoot);

                    search.FindAll();
                }
                catch (Exception ex)
                {
                    pingResult = false;
                }

                if (pingResult)
                {
                    return(pingResult);
                }
                else
                {
                    pingResult = false;
                }
                return(pingResult);
            }
            catch (Exception e)
            {
                Log.Error(e, e.Message);
                return(false);
            }
        }
Пример #2
0
        public List <DomainUser> FindAll(int id, string userName)
        {
            try
            {
                string cipherText = "";

                IQueryable <DomainSetting> domainSetting = _context.DomainSetting.Where(w => w.Id == id);
                var domain = domainSetting.Select(w => new DomainSetting
                {
                    UserName = w.UserName,
                    Server   = w.Server,
                    Title    = w.Title,
                    Password = w.Password
                }).FirstOrDefault();
                cipherText = _encriptdescriptStringRepository.DecryptString(domain.Password);
                List <DomainUser> lstADUsers     = new List <DomainUser>();
                DomainUser        objSurveyUsers = new DomainUser();
                string            dcString       = "";
                string            rootNode       = "";
                string[]          arrString;
                arrString = domain.Title.Split('.');
                if (arrString.Length == 1)
                {
                    dcString = "dc=" + domain.Title + "";
                    rootNode = arrString[0];
                }
                else
                {
                    for (int i = 0; i != arrString.Length; i++)
                    {
                        dcString += "dc=" + arrString[i].ToString() + ",";
                    }
                    if (arrString.Length == 3)
                    {
                        rootNode = arrString[1].ToString();
                    }
                    else if (arrString.Length == 2)
                    {
                        rootNode = arrString[0].ToString();
                    }
                    dcString = dcString.Substring(0, dcString.Length - 1);
                }
                string DomainPath = "LDAP://" + domain.Server + "/" + dcString;
                System.DirectoryServices.DirectoryEntry searchRoot = new System.DirectoryServices.DirectoryEntry(DomainPath);
                try
                {
                    object nativeObject = searchRoot.NativeObject;

                    searchRoot.Username = domain.UserName;
                    searchRoot.Password = cipherText;
                    DirectorySearcher search = new DirectorySearcher(searchRoot);
                    if (userName == "*")
                    {
                        search.Filter = $"(objectClass=user)";
                    }
                    else
                    {
                        userName      = userName.Split("@")[0];
                        search.Filter = $"(samaccountname=*{userName}*)";
                    }

                    search.PropertiesToLoad.Add("samaccountname");
                    search.PropertiesToLoad.Add("mail");
                    search.PropertiesToLoad.Add("usergroup");
                    search.PropertiesToLoad.Add("displayname"); //first name
                    search.PropertiesToLoad.Add("givenname");   //first name
                    search.PropertiesToLoad.Add("sn");          //first name
                    SearchResult resultFetch;


                    //SearchResultCollection resultCol = search.FindAll();
                    SearchResult resultCol = search.FindOne();

                    if (resultCol != null)
                    {
                        //for (int counter = 0; counter < resultCol.Count; counter++)
                        //{
                        string UserNameEmailString = string.Empty;
                        //resultFetch = resultCol[counter];
                        resultFetch = resultCol;
                        if (resultFetch.Properties.Contains("samaccountname"))
                        {
                            objSurveyUsers = new DomainUser();
                            if (resultFetch.Properties.Contains("mail"))
                            {
                                objSurveyUsers.Email = (String)resultFetch.Properties["mail"][0];
                            }
                            else
                            {
                                //  objSurveyUsers.Email = (String)resultFetch.Properties["samaccountname"][0] + id.ToString() + "@Pointer.com";
                            }

                            if (resultFetch.Properties.Contains("displayname"))
                            {
                                objSurveyUsers.DisplayName = (String)resultFetch.Properties["displayname"][0];
                            }

                            else
                            {
                                objSurveyUsers.DisplayName = (String)resultFetch.Properties["samaccountname"][0];
                            }


                            objSurveyUsers.UserName = (String)resultFetch.Properties["samaccountname"][0];

                            if (resultFetch.Properties.Contains("givenname"))
                            {
                                objSurveyUsers.FirstName = (String)resultFetch.Properties["givenname"][0];
                            }
                            else
                            {
                                objSurveyUsers.FirstName = (String)resultFetch.Properties["samaccountname"][0];
                            }
                            if (resultFetch.Properties.Contains("sn"))
                            {
                                objSurveyUsers.LastName = (String)resultFetch.Properties["sn"][0];
                            }
                            else
                            {
                                objSurveyUsers.LastName = (String)resultFetch.Properties["samaccountname"][0];
                            }
                            objSurveyUsers.dcString = dcString;
                            lstADUsers.Add(objSurveyUsers);
                        }
                    }
                }
                // }
                catch (Exception ex)
                {
                }
                return(lstADUsers);
            }
            catch (Exception e)
            {
                Log.Error(e, e.Message);
                return(null);
            }
        }
Пример #3
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            try
            {
                ViewData["ReturnUrl"] = returnUrl;
                if (ModelState.IsValid)
                {
                    if (model.UserName.Contains("@"))
                    {
                        bool authentic          = false;
                        List <DomainUser> users = _findAllADUsers.FindAll(model.DomainId, model.UserName);

                        ApplicationUser        applicationUser  = new ApplicationUser();
                        List <ApplicationUser> applicationUsers = new List <ApplicationUser>();

                        foreach (var user in users)
                        {
                            applicationUser.UserName             = user.DisplayName;
                            applicationUser.FirstName            = user.FirstName;
                            applicationUser.LastName             = user.LastName;
                            applicationUser.Email                = user.Email;
                            applicationUser.EmailConfirmed       = true;
                            applicationUser.PhoneNumberConfirmed = true;
                            applicationUser.PhoneNumber          = "-";
                            applicationUser.Mobile               = "-";
                            applicationUser.AddedDate            = DateTime.Now;
                            applicationUsers.Add(applicationUser);
                        }
                        IQueryable <DomainSetting> domainSetting = _context.DomainSetting.Where(w => w.Id == model.DomainId);
                        string cipherText = "";
                        var    domain     = domainSetting.Select(w => new DomainSetting
                        {
                            UserName = w.UserName,
                            Server   = w.Server,
                            Title    = w.Title,
                            Password = w.Password
                        }).FirstOrDefault();
                        cipherText = _encriptdescriptStringRepository.DecryptString(domain.Password);
                        string   dcString = "";
                        string   rootNode = "";
                        string[] arrString;
                        arrString = domain.Title.Split('.');
                        if (arrString.Length == 1)
                        {
                            dcString = "dc=" + domain.Title + "";
                            rootNode = arrString[0];
                        }
                        else
                        {
                            for (int i = 0; i != arrString.Length; i++)
                            {
                                dcString += "dc=" + arrString[i].ToString() + ",";
                            }
                            if (arrString.Length == 3)
                            {
                                rootNode = arrString[1].ToString();
                            }
                            else if (arrString.Length == 2)
                            {
                                rootNode = arrString[0].ToString();
                            }
                            dcString = dcString.Substring(0, dcString.Length - 1);
                        }
                        string         DomainPath = "LDAP://" + domain.Server + "/" + dcString;
                        DirectoryEntry entry      = new DirectoryEntry("LDAP://" + domain.Server + "/" + dcString, model.UserName.Split("@")[0], model.Password);
                        try
                        {
                            object nativeObject = entry.NativeObject;

                            authentic = true;
                            byte[] salt = new byte[128 / 8];
                            model.Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                                        password: "******",
                                                                        salt: salt,
                                                                        prf: KeyDerivationPrf.HMACSHA1,
                                                                        iterationCount: 10000,
                                                                        numBytesRequested: 256 / 8));
                            var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, true, false);

                            if (result.Succeeded)
                            {
                                return(RedirectToLocal(returnUrl));
                            }
                        }
                        catch (Exception e)
                        {
                            ModelState.AddModelError(string.Empty, Resources.Messages.InvalidLoginAttempt);
                            return(View(model));
                        }
                    }
                    else
                    {
                        IQueryable <DomainSetting> domainSetting = _context.DomainSetting.Where(w => w.Id == model.DomainId);

                        var domain = domainSetting.Select(w => new DomainSetting
                        {
                            Id    = w.Id,
                            Title = w.Title,
                        }).FirstOrDefault();

                        if (domain.Title == "کاربران سیستمی")
                        {
                            var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, true, false);

                            if (result.Succeeded)
                            {
                                return(RedirectToLocal(returnUrl));
                            }
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, Resources.Messages.InvaliDomainAttempt);
                            return(View(model));
                        }
                    }


                    ModelState.AddModelError(string.Empty, Resources.Messages.InvalidLoginAttempt);
                    return(View(model));
                }

                // If we got this far, something failed, redisplay form
                return(View(model));
            }
            catch (Exception e)
            {
                Log.Error(e, e.Message);
                return(View("~/Views/Shared/Error.cshtml", new ErrorViewModel {
                    RequestId = e.Message
                }));
            }
        }
Пример #4
0
        public IActionResult Index()
        {
            try
            {
                int    DomainId = 0;
                string Password = "";
                // var items = _domainRepository.GetAsQueryable(x => HasAccess1(x.Id)).ToList();
                var items = _domainRepository.GetAll().ToList();
                var model = _mapper.Map <IEnumerable <DomainSetting>, IEnumerable <DomainSettingViewModel> >(items);

                DomainSettingViewModel m = new DomainSettingViewModel();

                foreach (var x in model)
                {
                    m.Id       = x.Id;
                    m.Server   = x.Server;
                    m.Title    = x.Title;
                    m.IsActive = x.IsActive;
                    m.Password = x.Password;
                }
                IQueryable <DomainSetting> domainSetting = _context.DomainSetting.Where(w => w.Title != "کاربران سیستمی");
                var domain = domainSetting.Select(w => new DomainSetting
                {
                    Id       = w.Id,
                    Server   = w.Server,
                    Title    = w.Title,
                    UserName = w.UserName,
                    Password = w.Password
                }).FirstOrDefault();
                if (domain != null)
                {
                    string cipherText = "";
                    cipherText = _encriptdescriptStringRepository.DecryptString(domain.Password);
                    DomainId   = domain.Id;
                    Password   = cipherText;
                    bool ping = _pingLdap.Ping(DomainId, Password);
                    if (ping)
                    {
                        if (m.Title != "کاربران سیستمی")
                        {
                            m.IsActive = true;
                        }
                        foreach (var y in model)
                        {
                            if (y.Title != "کاربران سیستمی")
                            {
                                y.IsActive = m.IsActive;
                            }
                        }
                    }
                    return(View(model));
                }
                else
                {
                    return(View(model));
                }
            }
            catch (Exception e)
            {
                Log.Error(e, e.Message);
                return(View("~/Views/Shared/Error.cshtml", new ErrorViewModel {
                    RequestId = e.Message
                }));
            }
        }