예제 #1
0
        public void LocatorFlag_Set_GetReturnsExpected(LocatorFlags value)
        {
            LdapSessionOptions options = new LdapConnection("server").SessionOptions;

            Assert.Equal(LocatorFlags.None, options.LocatorFlag);

            options.LocatorFlag = value;
            Assert.Equal(value, options.LocatorFlag);
        }
        private ADServerInfo GetDirectoryServer(string partitionFqdn, ADRole role)
        {
            ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, ADRole>((long)this.GetHashCode(), "GetDirectoryServer PartitionFqdn {0}. Role {1}", partitionFqdn, role);
            LocatorFlags locatorFlags = LocatorFlags.ForceRediscovery | LocatorFlags.DirectoryServicesRequired | LocatorFlags.ReturnDnsName;
            string       text         = partitionFqdn;

            if (ADRole.GlobalCatalog == role)
            {
                ADObjectId rootDomainNamingContext = base.GetRootDomainNamingContext(partitionFqdn);
                ADObjectId domainNamingContext     = base.GetDomainNamingContext(partitionFqdn);
                if (!rootDomainNamingContext.DistinguishedName.Equals(domainNamingContext.DistinguishedName, StringComparison.OrdinalIgnoreCase))
                {
                    text = NativeHelpers.CanonicalNameFromDistinguishedName(rootDomainNamingContext.DistinguishedName);
                }
                locatorFlags |= LocatorFlags.GCRequired;
            }
            ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, string, LocatorFlags>((long)this.GetHashCode(), "GetDirectoryServer. Partition Fqdn {0} Parent Domain {1}. Flags {2}", partitionFqdn, text, locatorFlags);
            ADServerInfo         serverInfo           = new ADServerInfo(null, text, (ADRole.GlobalCatalog == role) ? 3268 : 389, null, 100, AuthType.Kerberos, true);
            PooledLdapConnection pooledLdapConnection = null;
            ADServerInfo         adserverInfo         = null;

            try
            {
                pooledLdapConnection = LdapConnectionPool.CreateOneTimeConnection(null, serverInfo, locatorFlags);
                if (!string.IsNullOrEmpty(pooledLdapConnection.SessionOptions.HostName))
                {
                    adserverInfo = pooledLdapConnection.ADServerInfo.CloneWithServerNameResolved(pooledLdapConnection.SessionOptions.HostName);
                }
                ExTraceGlobals.TopologyProviderTracer.TraceDebug <string, string>((long)this.GetHashCode(), "GetDirectoryServer. Partition Fqdn {0}. Server {1}", partitionFqdn, pooledLdapConnection.SessionOptions.HostName ?? string.Empty);
            }
            finally
            {
                if (pooledLdapConnection != null)
                {
                    pooledLdapConnection.ReturnToPool();
                }
            }
            string          text2;
            LocalizedString localizedString;

            if (adserverInfo != null && SuitabilityVerifier.IsServerSuitableIgnoreExceptions(adserverInfo.Fqdn, ADRole.GlobalCatalog == role, null, out text2, out localizedString))
            {
                return(adserverInfo);
            }
            return(LdapTopologyProvider.FindDirectoryServerForForestOrDomain(text, null, ADRole.GlobalCatalog == role));
        }
        // Token: 0x06000DCB RID: 3531 RVA: 0x0003F978 File Offset: 0x0003DB78
        internal static PooledLdapConnection CreateOneTimeConnection(NetworkCredential networkCredential, ADServerInfo serverInfo, LocatorFlags connectionFlags = LocatorFlags.None)
        {
            string arg = "<null>\\<null>";

            if (networkCredential != null)
            {
                arg = networkCredential.Domain + "\\" + networkCredential.UserName;
            }
            else
            {
                using (WindowsIdentity current = WindowsIdentity.GetCurrent())
                {
                    if (current.ImpersonationLevel == TokenImpersonationLevel.Delegation || current.ImpersonationLevel == TokenImpersonationLevel.Impersonation)
                    {
                        arg = current.Name;
                    }
                }
            }
            ExTraceGlobals.ConnectionTracer.TraceDebug <string, string>(0L, "LdapConnectionPool::CreateOneTimeConnection - opening new ONE-TIME PooledLdapConnection to {0} as {1}", serverInfo.FqdnPlusPort, arg);
            ADProviderPerf.AddDCInstance(serverInfo.Fqdn);
            ADServerRole         role = (serverInfo.Port == 389) ? ADServerRole.DomainController : ADServerRole.GlobalCatalog;
            bool                 flag = false;
            PooledLdapConnection pooledLdapConnection = null;
            PooledLdapConnection result;

            try
            {
                pooledLdapConnection = new PooledLdapConnection(serverInfo, role, false, networkCredential);
                if (LocatorFlags.None != connectionFlags)
                {
                    pooledLdapConnection.SessionOptions.LocatorFlag |= connectionFlags;
                }
                Globals.LogEvent(DirectoryEventLogConstants.Tuple_DSC_EVENT_NEW_CONNECTION, null, new object[]
                {
                    serverInfo.Fqdn,
                    serverInfo.Port,
                    string.Empty
                });
                pooledLdapConnection.BindWithRetry(3);
                pooledLdapConnection.SetNamingContexts();
                flag   = true;
                result = pooledLdapConnection;
            }
            catch (LdapException ex)
            {
                throw new ADTransientException(DirectoryStrings.ExceptionCreateLdapConnection(serverInfo.FqdnPlusPort, ex.Message, (uint)ex.ErrorCode), ex);
            }
            finally
            {
                if (!flag && pooledLdapConnection != null)
                {
                    pooledLdapConnection.ReturnToPool();
                }
            }
            return(result);
        }