当前位置: 首页>>代码示例>>C#>>正文


C# DirectoryEntry.Dispose方法代码示例

本文整理汇总了C#中DirectoryEntry.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryEntry.Dispose方法的具体用法?C# DirectoryEntry.Dispose怎么用?C# DirectoryEntry.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DirectoryEntry的用法示例。


在下文中一共展示了DirectoryEntry.Dispose方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateApplicationPartition

        private void CreateApplicationPartition(string distinguishedName, string objectClass)
        {
            if (_appType == ApplicationPartitionType.ADApplicationPartition)
            {
                //
                // AD
                // 1. Bind to the non-existent application partition using the fast bind and delegation option
                // 2. Get the Parent object and create a new "domainDNS" object under it
                // 3. Set the instanceType and the description for the application partitin object
                //

                DirectoryEntry tempEntry = null;
                DirectoryEntry parent = null;

                try
                {
                    AuthenticationTypes authType = Utils.DefaultAuthType | AuthenticationTypes.FastBind | AuthenticationTypes.Delegation;

                    if (DirectoryContext.ServerBindSupported)
                    {
                        authType |= AuthenticationTypes.ServerBind;
                    }

                    tempEntry = new DirectoryEntry("LDAP://" + context.GetServerName() + "/" + distinguishedName, context.UserName, context.Password, authType);
                    parent = tempEntry.Parent;
                    _domainDNSEntry = parent.Children.Add(Utils.GetRdnFromDN(distinguishedName), PropertyManager.DomainDNS);
                    // set the instance type to 5
                    _domainDNSEntry.Properties[PropertyManager.InstanceType].Value = NCFlags.InstanceTypeIsNCHead | NCFlags.InstanceTypeIsWriteable;
                    // mark this as uncommitted
                    _committed = false;
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
                finally
                {
                    // dispose all resources
                    if (parent != null)
                    {
                        parent.Dispose();
                    }

                    if (tempEntry != null)
                    {
                        tempEntry.Dispose();
                    }
                }
            }
            else
            {
                //
                // ADAM
                // 1. Bind to the partitions container on the domain naming owner instance
                // 2. Create a disabled crossRef object for the new application partition
                // 3. Bind to the target hint and follow the same steps as for AD
                //

                try
                {
                    InitializeCrossRef(distinguishedName);

                    DirectoryEntry tempEntry = null;
                    DirectoryEntry parent = null;

                    try
                    {
                        AuthenticationTypes authType = Utils.DefaultAuthType | AuthenticationTypes.FastBind;

                        if (DirectoryContext.ServerBindSupported)
                        {
                            authType |= AuthenticationTypes.ServerBind;
                        }
                        tempEntry = new DirectoryEntry("LDAP://" + context.Name + "/" + distinguishedName, context.UserName, context.Password, authType);
                        parent = tempEntry.Parent;
                        _domainDNSEntry = parent.Children.Add(Utils.GetRdnFromDN(distinguishedName), objectClass);

                        // set the instance type to 5
                        _domainDNSEntry.Properties[PropertyManager.InstanceType].Value = NCFlags.InstanceTypeIsNCHead | NCFlags.InstanceTypeIsWriteable;

                        // mark this as uncommitted
                        _committed = false;
                    }
                    finally
                    {
                        // dispose all resources
                        if (parent != null)
                        {
                            parent.Dispose();
                        }

                        if (tempEntry != null)
                        {
                            tempEntry.Dispose();
                        }
                    }
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
//.........这里部分代码省略.........
开发者ID:chcosta,项目名称:corefx,代码行数:101,代码来源:ApplicationPartition.cs

示例2: IsContextValid


//.........这里部分代码省略.........
                    // we can get this error if the target it server:port (not a valid forest)
                    contextIsValid = false;
                }
                else if (errorCode != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
                else
                {
                    Debug.Assert(domainControllerInfo != null);
                    Debug.Assert(domainControllerInfo.DnsForestName != null);
                    context.serverName = domainControllerInfo.DnsForestName;
                    contextIsValid = true;
                }
            }
            else if (contextType == DirectoryContextType.ApplicationPartition)
            {
                Debug.Assert(context.Name != null);

                // check for application partition
                int errorCode = 0;
                DomainControllerInfo domainControllerInfo;
                errorCode = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)PrivateLocatorFlags.OnlyLDAPNeeded, out domainControllerInfo);

                if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                {
                    // try with force rediscovery 

                    errorCode = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)PrivateLocatorFlags.OnlyLDAPNeeded | (long)LocatorOptions.ForceRediscovery, out domainControllerInfo);

                    if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                    {
                        contextIsValid = false;
                    }
                    else if (errorCode != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                    }
                    else
                    {
                        contextIsValid = true;
                    }
                }
                else if (errorCode == NativeMethods.ERROR_INVALID_DOMAIN_NAME_FORMAT)
                {
                    // we can get this error if the target it server:port (not a valid application partition)
                    contextIsValid = false;
                }
                else if (errorCode != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
                else
                {
                    contextIsValid = true;
                }
            }
            else if (contextType == DirectoryContextType.DirectoryServer)
            {
                //
                // if the servername contains a port number, then remove that
                //
                string tempServerName = null;
                string portNumber;
                tempServerName = Utils.SplitServerNameAndPortNumber(context.Name, out portNumber);

                //
                // this will validate that the name specified in the context is truely the name of a machine (and not of a domain)
                //
                DirectoryEntry de = new DirectoryEntry("WinNT://" + tempServerName + ",computer", context.UserName, context.Password, Utils.DefaultAuthType);
                try
                {
                    de.Bind(true);
                    contextIsValid = true;
                }
                catch (COMException e)
                {
                    if ((e.ErrorCode == unchecked((int)0x80070035)) || (e.ErrorCode == unchecked((int)0x80070033)) || (e.ErrorCode == unchecked((int)0x80005000)))
                    {
                        // if this returns bad network path 
                        contextIsValid = false;
                    }
                    else
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }
                }
                finally
                {
                    de.Dispose();
                }
            }
            else
            {
                // no special validation for ConfigurationSet
                contextIsValid = true;
            }

            return contextIsValid;
        }
开发者ID:chcosta,项目名称:corefx,代码行数:101,代码来源:DirectoryContext.cs

示例3: LookForUserInAllDomains

    public static DataTable LookForUserInAllDomains(string sLastNameSearch, string sFirstNameSearch)
    {
        if (sUID == "") sUID = null;
        if (sPwd == "") sPwd = null;

        CreateNetworkUserTable();
        objTable.Rows.Clear();

        ////Search in all the domains
        //string ldapdomains = System.Configuration.ConfigurationManager.AppSettings["LDAPDomains"].ToString();
        //string[] Domains = ldapdomains.Split(new char[] { ';' });

        //for (int i = 0; i < Domains.Length; i++)
        //{
        //    string domainName = Domains[i];

        //    objTable = LookForUserInDomain(domainName, sLastNameSearch, sFirstNameSearch);

        //}

        string sFilter = String.Format("(|(&(objectClass=User)(givenname={0})(sn={1})))", sFirstNameSearch, sLastNameSearch);

        // collect inactive users in all the domains
        string[] sDomains = sLDAPDomains.Split(new char[] { ';' });
        for (int i = 0; i < sDomains.Length; i++ )
        {
            string sDomainName = sDomains[ i ];
            string sServerName = System.Configuration.ConfigurationManager.AppSettings[sDomainName].ToString();
            string sLDAPPath = "LDAP://" + sServerName + "/DC=" + sDomainName + ",DC=root01,DC=org";

            DirectoryEntry objRootDE = new DirectoryEntry(sLDAPPath, sUID, sPwd, AuthenticationTypes.Secure);
            DirectorySearcher objDS = new DirectorySearcher(objRootDE);

            objDS.Filter = sFilter;
            objDS.ReferralChasing = ReferralChasingOption.None;
            objDS.PropertiesToLoad.Add("userAccountControl");
            objDS.PropertiesToLoad.Add("SAMAccountName");
            objDS.PropertiesToLoad.Add("givenName");
            objDS.PropertiesToLoad.Add("sn");
            objDS.PropertiesToLoad.Add("TelephoneNumber");
            objDS.PropertiesToLoad.Add("mail");

            SearchResultCollection objSRC = null;
            try
            {
                objSRC = objDS.FindAll();
            }
            catch (Exception excpt)
            {
                if (excpt.Message.IndexOf("The server is not operational.") < 0)
                    throw;
            }

            if (objSRC == null)
                continue;

            foreach (SearchResult objSR in objSRC)
            {
                int iInactiveFlag	= Convert.ToInt32(objSR.Properties["userAccountControl"][0]);
                string sUserId		= objSR.Properties["SAMAccountName"][0].ToString();
                string sFirstName	= objSR.Properties["givenName"][0].ToString();
                string sLastName	= objSR.Properties["sn"][0].ToString();

                string sPhone	= "";
                string sEmail	= "";

                if (objSR.Properties["TelephoneNumber"].Count > 0)
                    sPhone	= objSR.Properties["TelephoneNumber"][0].ToString();

                if( objSR.Properties["mail"].Count > 0 )
                    sEmail	= objSR.Properties["mail"][0].ToString();

                iInactiveFlag = iInactiveFlag & 0x0002;
                if (iInactiveFlag <= 0)
                {
                    // add name, username, phone and email to the table, if active
                    DataRow objRow = objTable.NewRow();

                    objRow["LastName"] = sLastName;
                    objRow["FirstName"] = sFirstName;
                    objRow["Username"] = sUserId;
                    objRow["UserDomain"] = sDomainName;
                    objRow["Phone"] = sPhone;
                    objRow["Email"] = sEmail;

                    objTable.Rows.Add( objRow );

                    continue;
                }
            }

            objSRC.Dispose();
            objDS.Dispose();
            objRootDE.Close();
            objRootDE.Dispose();
        }

        return objTable;
    }
开发者ID:fmendes,项目名称:ActiveDirectory,代码行数:99,代码来源:NetworkUsersMapping.cs

示例4: isAuth

    public bool isAuth(string domain, string username, string password)
    {
        if (domain.Trim().Length == 0)
                return false;
            if (username.Trim().Length == 0)
                return false;
            if (password.Trim().Length == 0)
                return false;

            if (_path == "")
                _path = @"LDAP://DC=" + domain + ",DC=ad,DC=flextronics,DC=com";
            string domain_user = domain + "\\" + username;
            //DirectoryEntry entry = new DirectoryEntry(_path, domain + @"\" + username, password);
            DirectoryEntry entry = new DirectoryEntry(_path,domain_user , password);
            try
            {
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + username + ")";
                return __defineUser(ref search);
            }
            catch (Exception ex)
            {
                message = "Error authenticating user." + ex.Message;
                return false;
            }
            finally
            {
                entry.Dispose();
            }
    }
开发者ID:rivernli,项目名称:pMKT,代码行数:30,代码来源:LDAP.cs

示例5: RetrieveAllNetworkUsersFromLDAP

    private static SearchResultCollection RetrieveAllNetworkUsersFromLDAP(string sDomainName)
    {
        string sServerName = System.Configuration.ConfigurationManager.AppSettings[sDomainName].ToString();
        string sLDAPPath = "LDAP://" + sServerName + "/DC=" + sDomainName + ",DC=root01,DC=org";

        DirectoryEntry objRootDE = new DirectoryEntry(sLDAPPath, sUID, sPwd, AuthenticationTypes.Secure);
        DirectorySearcher objDS = new DirectorySearcher(objRootDE);

        objDS.Filter = "(|(&(objectClass=User)(givenname=*)(sn=*)))";
        objDS.ReferralChasing = ReferralChasingOption.None;
        objDS.PropertiesToLoad.Add("userAccountControl");
        objDS.PropertiesToLoad.Add("SAMAccountName");

        SearchResultCollection objSRC = null;
        try
        {
            objSRC = objDS.FindAll();
        }
        catch (Exception excpt)
        {
            if (excpt.Message.IndexOf("The server is not operational.") < 0)
                throw;
        }

        objDS.Dispose();
        objRootDE.Close();
        objRootDE.Dispose();
        return objSRC;
    }
开发者ID:fmendes,项目名称:ActiveDirectory,代码行数:29,代码来源:NetworkUsersMapping.cs


注:本文中的DirectoryEntry.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。