예제 #1
0
        private void GetDCInfo(string domain)
        {
            CNetlogon.LWNET_DC_INFO DCInfo;

            if (String.IsNullOrEmpty(domain))
            {
                CNetlogon.GetCurrentDomain(out sDomain);
            }
            else
            {
                sDomain = domain;
            }

            uint netlogonError = CNetlogon.GetDCName(sDomain, 0, out DCInfo);

            if (netlogonError == 0 && !String.IsNullOrEmpty(DCInfo.DomainControllerName))
            {
                sDomainController = DCInfo.DomainControllerName;
            }

            if (netlogonError == 0 && !String.IsNullOrEmpty(DCInfo.FullyQualifiedDomainName))
            {
                sDomain = DCInfo.FullyQualifiedDomainName;
            }
        }
예제 #2
0
        public static void ReadRemoteHostFQDN(string hostname, out string hostFQDN)
        {
            hostFQDN = string.Empty; string domain = string.Empty;

            uint error = CNetlogon.GetCurrentDomain(out domain);

            if (error != 0 && String.IsNullOrEmpty(domain))
            {
                return;
            }

            string[] rootDNcom = domain.Split('.');

            string rootDN = ""; string errorMessage = "";

            foreach (string str in rootDNcom)
            {
                string temp = string.Concat("dc=", str, ",");
                rootDN = string.Concat(rootDN, temp);
            }
            rootDN = rootDN.Substring(0, rootDN.Length - 1);

            try
            {
                DirectoryContext dirContext = DirectoryContext.CreateDirectoryContext
                                                  (domain,
                                                  rootDN,
                                                  null,
                                                  null,
                                                  389,
                                                  false,
                                                  out errorMessage);

                if (!String.IsNullOrEmpty(errorMessage))
                {
                    Logger.ShowUserError(errorMessage);
                }

                if (dirContext == null)
                {
                    return;
                }

                List <LdapEntry> ldapEntries = new List <LdapEntry>();

                string[] attrs = { "name", "dNSHostName", null };

                int ret = dirContext.ListChildEntriesSynchronous
                              (rootDN,
                              LdapAPI.LDAPSCOPE.SUB_TREE,
                              string.Format("(&(objectClass=computer)(cn={0}))", hostname),
                              attrs,
                              false,
                              out ldapEntries);

                if (ldapEntries == null)
                {
                    return;
                }

                LdapEntry ldapNextEntry = ldapEntries[0];

                string[] attrsList = ldapNextEntry.GetAttributeNames();

                Logger.Log("The number of attributes are " + attrsList.Length, Logger.ldapLogLevel);

                if (attrsList != null)
                {
                    foreach (string attr in attrsList)
                    {
                        if (attr.Trim().Equals("dNSHostName"))
                        {
                            LdapValue[] attrValues = ldapNextEntry.GetAttributeValues(attr, dirContext);
                            if (attrValues != null && attrValues.Length > 0)
                            {
                                hostFQDN = attrValues[0].stringData;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                hostFQDN = string.Empty;
                Logger.LogException("EventAPI.ReadRemoteHostFQDN", ex);
            }
        }