.Net Directory Classes give COM errors

  • Thread starter Thread starter Waldy
  • Start date Start date
W

Waldy

Hi there,
does anyone have any idea why I cannot use the .Net Active
Directory classes? COM errors occur when I try to use them. I
can't even use the static method DirectoryEntry.Exists without a COM
Exception (Unknown Error). This is with Visual Studio 2005. I also have
2003 and 2008 installed, would that make a difference?
 
Hello "Waldy"

I use these classes with success in my environments ( VS 2005 and 2008 ) so
my first guesses are that
1. it is a user rights issue
2. AD is not setup correctly on the network

regards

Michel
 
Here is a class i wrote that works perfectly on our network in both
environments , it might just get you started

Imports System.DirectoryServices

Public Class ADQuery

Implements IDisposable

Private _DirEntry As DirectoryEntry

Public Property DirEntry() As DirectoryEntry

Get

Return _DirEntry

End Get

Private Set(ByVal value As DirectoryEntry)

_DirEntry = value

End Set

End Property

''' <summary>

''' Initializes a new instance of the <see cref="ADQuery" /> class.

''' </summary>

''' <param name="DomainName">Name of the domain.</param>

Public Sub New(ByVal DomainName As String)

DirEntry = New DirectoryEntry(String.Concat("LDAP://", DomainName)) 'connect
to direrctory

DirSearch = New DirectorySearcher(DirEntry)





End Sub

Private _DirSearch As DirectorySearcher

Public Property DirSearch() As DirectorySearcher

Get

Return _DirSearch

End Get

Private Set(ByVal value As DirectorySearcher)

_DirSearch = value

End Set

End Property

''' <summary>

''' Mails the adress from LDAP.

''' </summary>

''' <param name="sAMAccountName">Name of the s AM account.</param>

''' <returns></returns>

Public Function MailAdressFromLDAP(ByVal sAMAccountName As String) As String

Dim ret As String = String.Empty 'declare ret var

DirSearch.Filter = String.Concat("(&(objectClass=user)(sAMAccountName=",
sAMAccountName, "))") 'filter relevant info

Dim oResult As SearchResult = DirSearch.FindOne

ret = oResult.GetDirectoryEntry().Properties("mail").Value.ToString

Return ret ' return the retvar

End Function

Public Structure stFullName

''' <summary>

'''

''' </summary>

Dim FirstName As String

Dim LastName As String

End Structure

''' <summary>

''' Fulls the name from LDAP.

''' </summary>

''' <param name="sAMAccountName">Name of the s AM account.</param>

''' <returns></returns>

Public Function FullNameFromLDAP(ByVal sAMAccountName As String) As
stFullName

Dim Ret As stFullName = Nothing

DirSearch.Filter = String.Concat("(&(objectClass=user)(sAMAccountName=",
sAMAccountName, "))") 'filter relevant info

Dim oResult As SearchResult = DirSearch.FindOne

With Ret

Try

..FirstName =
oResult.GetDirectoryEntry().Properties("givenName").Value.ToString

..LastName = oResult.GetDirectoryEntry().Properties("sn").Value.ToString

Catch ex As Exception

End Try

End With

Return Ret

End Function

#Region " IDisposable Support "

Private disposedValue As Boolean = False ' To detect redundant calls

''' <summary>

''' Releases unmanaged and - optionally - managed resources

''' </summary>

''' <param name="disposing"><c>true</c> to release both managed and
unmanaged resources; <c>false</c> to release only unmanaged
resources.</param>

Protected Overridable Sub Dispose(ByVal disposing As Boolean)

If Not Me.disposedValue Then

If disposing Then

If DirEntry IsNot Nothing Then

DirEntry.Dispose()

End If

If DirSearch IsNot Nothing Then

DirSearch.Dispose()

End If

' TODO: free other state (managed objects).

End If

' TODO: free your own state (unmanaged objects).

' TODO: set large fields to null.

End If

Me.disposedValue = True

End Sub

''' <summary>

''' Performs application-defined tasks associated with freeing, releasing,
or resetting unmanaged resources.

''' </summary>

Public Sub Dispose() Implements IDisposable.Dispose

' Do not change this code. Put cleanup code in Dispose(ByVal disposing As
Boolean) above.

Dispose(True)

GC.SuppressFinalize(Me)

End Sub

#End Region

End Class

C# Version


using System.DirectoryServices;
public class ADQuery : IDisposable
{
private DirectoryEntry _DirEntry;
public DirectoryEntry DirEntry {
get { return _DirEntry; }
private set { _DirEntry = value; }
}

/// <summary>
/// Initializes a new instance of the <see cref="ADQuery" /> class.
/// </summary>
/// <param name="DomainName">Name of the domain.</param>
public ADQuery(string DomainName)
{
DirEntry = new DirectoryEntry(string.Concat("LDAP://", DomainName));
//connect to direrctory
DirSearch = new DirectorySearcher(DirEntry);



}
private DirectorySearcher _DirSearch;
public DirectorySearcher DirSearch {
get { return _DirSearch; }
private set { _DirSearch = value; }
}
/// <summary>
/// Mails the adress from LDAP.
/// </summary>
/// <param name="sAMAccountName">Name of the s AM account.</param>
/// <returns></returns>
public string MailAdressFromLDAP(string sAMAccountName)
{
string ret = string.Empty;
//declare ret var
DirSearch.Filter =
string.Concat("(&(objectClass=user)(sAMAccountName=", sAMAccountName, "))");
//filter relevant info
SearchResult oResult = DirSearch.FindOne;
ret = oResult.GetDirectoryEntry().Properties("mail").Value.ToString;
return ret;
// return the retvar
}
public struct stFullName
{
/// <summary>
///
/// </summary>
public string FirstName;
public string LastName;
}
/// <summary>
/// Fulls the name from LDAP.
/// </summary>
/// <param name="sAMAccountName">Name of the s AM account.</param>
/// <returns></returns>
public stFullName FullNameFromLDAP(string sAMAccountName)
{
stFullName Ret = null;
DirSearch.Filter =
string.Concat("(&(objectClass=user)(sAMAccountName=", sAMAccountName, "))");
//filter relevant info
SearchResult oResult = DirSearch.FindOne;
{
try {
Ret.FirstName =
oResult.GetDirectoryEntry().Properties("givenName").Value.ToString;
Ret.LastName =
oResult.GetDirectoryEntry().Properties("sn").Value.ToString;
}
catch (Exception ex) {
}

}
return Ret;
}
#region " IDisposable Support "
private bool disposedValue = false;
// To detect redundant calls
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and
unmanaged resources; <c>false</c> to release only unmanaged
resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue) {
if (disposing) {
if (DirEntry != null) {
DirEntry.Dispose();
}
if (DirSearch != null) {
DirSearch.Dispose();
}
}
// TODO: free other state (managed objects).
}

// TODO: free your own state (unmanaged objects).
// TODO: set large fields to null.
this.disposedValue = true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing,
releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(true);
GC.SuppressFinalize(this);
}
}
#endregion








regards

Michel
 
Back
Top