public int Get_FindUser_DisplayNameByFieldCode(ActiveDirectoryDTO ActiveDirectoryDTO)
        {
            DirectoryEntry dirEntry = ConnectToActiveForSearchStudent(ActiveDirectoryDTO.FieldCode);

            try
            {
                DirectorySearcher deSearch = new DirectorySearcher();

                deSearch.SearchRoot = dirEntry;

                deSearch.Filter = string.Format("(&(objectCategory=person)(anr={0}))", ActiveDirectoryDTO.DisplayName);
                // "(&(objectClass=user) (cn=a_rohani))";

                int result = deSearch.FindAll().Count;

                if (result != null)
                {
                    //DirectoryEntry deUser = new DirectoryEntry(result.Path);

                    //deUser.Close();
                    return(result);
                }
                else
                {
                    return(0);
                }
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                return(-1);
            }
        }
        public string CreateDisplayName(ActiveDirectoryDTO ActiveDirectoryDTO)
        {
            string fname = ActiveDirectoryDTO.Name;

            for (int i = 0; i <= Get_FindUser_DisplayNameByFieldCode(ActiveDirectoryDTO); i++)
            {
                fname = fname + " ";
            }
            ActiveDirectoryDTO.DisplayName = fname + " " + ActiveDirectoryDTO.sn;
            return(ActiveDirectoryDTO.DisplayName);
        }
        public bool CreateUser(ActiveDirectoryDTO ActiveDirectoryDTO, out string error)
        {
            string oGUID = string.Empty;

            error = "";
            try
            {
                DirectoryEntry dirEntry = ConnectToActiveForSearchStudent(ActiveDirectoryDTO.FieldCode);
                // oGUID = dirEntry.Guid.ToString();

                DirectoryEntry newUser = dirEntry.Children.Add("CN=" + ActiveDirectoryDTO.SamAccountName, "user");
                newUser.Properties["samAccountName"].Value    = ActiveDirectoryDTO.SamAccountName;
                newUser.Properties["userPrincipalName"].Value = ActiveDirectoryDTO.SamAccountName + "@iauec.local";
                newUser.Properties["uid"].Value         = ActiveDirectoryDTO.Uid;
                newUser.Properties["givenName"].Value   = ActiveDirectoryDTO.Name.Trim();
                newUser.Properties["displayName"].Value = CreateDisplayName(ActiveDirectoryDTO);
                newUser.Properties["name"].Value        = ActiveDirectoryDTO.Name.Trim();
                newUser.Properties["sn"].Value          = ActiveDirectoryDTO.sn.Trim();
                newUser.Properties["description"].Value = ActiveDirectoryDTO.Description.Trim();
                newUser.Properties["company"].Value     = ActiveDirectoryDTO.Company.Trim();
                newUser.Properties["department"].Value  = ActiveDirectoryDTO.Department.Trim();
                error += "136,";
                newUser.CommitChanges();
                error += "138,";
                oGUID  = newUser.Guid.ToString();
                error += "140,";
                newUser.Invoke("SetPassword", new object[] { "t123@456" });
                error += "142,";
                newUser.CommitChanges();
                error += "144,";
                newUser.Properties["userAccountControl"].Value = 0x200;
                error += "146,";
                newUser.CommitChanges();
                error += "148,";
                dirEntry.Close();
                newUser.Close();
                return(true);
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                oGUID  = E.Message.ToString();
                error += "156," + E.Message;
                return(false);
            }
            catch (Exception ex)
            {
                error += "162," + ex.Message;
                return(false);
            }
        }
        public bool CreateUser_ActiveDirectory(string stcode, out string error)
        {
            try
            {
                ActiveDirectoryDTO  ActiveDirectoryDTO = new ActiveDirectoryDTO();
                Email_ClassBusiness emb = new Email_ClassBusiness();

                DataTable DT = emb.GetStudentInfoFromAmozesh(stcode);
                string    level;
                if (DT.Rows[0]["magh"].ToString() == "1")
                {
                    level = "کارشناسی";
                }
                else if (DT.Rows[0]["magh"].ToString() == "5")
                {
                    level = "ارشد";
                }
                else
                {
                    level = "دکتری";
                }

                ActiveDirectoryDTO.SamAccountName = DT.Rows[0]["Email_Address"].ToString();
                ActiveDirectoryDTO.Uid            = DT.Rows[0]["Email_Address"].ToString();
                ActiveDirectoryDTO.GivenName      = DT.Rows[0]["name"].ToString();
                ActiveDirectoryDTO.DisplayName    = DT.Rows[0]["name"].ToString() + " " + DT.Rows[0]["family"].ToString();
                ActiveDirectoryDTO.Name           = DT.Rows[0]["name"].ToString();
                ActiveDirectoryDTO.sn             = DT.Rows[0]["family"].ToString();   //+ DT.Rows[0]["family"].ToString();
                ActiveDirectoryDTO.Description    = stcode;
                ActiveDirectoryDTO.Company        = DT.Rows[0]["nameresh"].ToString(); // +"-" + Field;
                ActiveDirectoryDTO.Department     = level;
                ActiveDirectoryDTO.Password       = "******";
                ActiveDirectoryDTO.FieldCode      = int.Parse(DT.Rows[0]["fresh_ID"].ToString());
                return(ActiveDirectoryBusiness.CreateUser(ActiveDirectoryDTO, out error));
            }
            catch (Exception ex)
            {
                error = "error112:" + ex.Message;
                return(false);
            }



            //Email_ClassBusiness emb = new Email_ClassBusiness();
            //DataTable DT =emb.GiveStudent_Active(stcode);
            //string Field;
            //if (DT.Rows[0]["magh"].ToString() == "1")
            //    Field = "کارشناسی";
            //else
            //    Field = "ارشد";
            //string ldapPath = "192.168.1.2/OU=rdv,OU=iauec,DC=iauec,DC=LOCAL";
            //string oGUID = string.Empty;

            //try
            //{
            //    string connectionPrefix = "LDAP://" + ldapPath;
            //    DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
            //    dirEntry.Username = "******";
            //    dirEntry.Password = "";
            //    oGUID = dirEntry.Guid.ToString(); // وقتی بخواهیم چک کنیم که آیا به درستی وصل شده به سرور

            //    DirectoryEntry newUser = dirEntry.Children.Add("CN=" + DT.Rows[0]["Email_Address"].ToString(), "user");
            //    newUser.Properties["samAccountName"].Value = DT.Rows[0]["Email_Address"].ToString();
            //    newUser.Properties["uid"].Value = DT.Rows[0]["Email_Address"].ToString();
            //    newUser.Properties["givenName"].Value = DT.Rows[0]["name"].ToString();
            //    newUser.Properties["description"].Value = stcode;
            //    newUser.Properties["company"].Value = DT.Rows[0]["nameresh"].ToString() + "-" + Field;
            //    newUser.Properties["displayName"].Value = DT.Rows[0]["family"].ToString(); //Farsi
            //    newUser.Properties["name"].Value = DT.Rows[0]["family"].ToString() + DT.Rows[0]["family"].ToString();
            //    newUser.CommitChanges();
            //    oGUID = newUser.Guid.ToString();
            //    //newUser.Invoke("SetPassword", new object[] { DT.Rows[0]["Password"].ToString() });
            //    newUser.Invoke("SetPassword", new object[] { "t123@456" });
            //    newUser.CommitChanges();
            //    newUser.Properties["userAccountControl"].Value = 0x200;
            //    newUser.CommitChanges();
            //    dirEntry.Close();
            //    newUser.Close();
            //    return true;
            //}
            //catch (System.DirectoryServices.DirectoryServicesCOMException E)
            //{
            //    oGUID = E.Message.ToString();
            //    return false;
            //}
        }