Problem using DirectoryEntry objects

Problem using DirectoryEntry objects

Post by Ragz » Sun, 11 May 2003 01:49:08



I am trying to retrieve the full name of a user using the
login name. The relevant part of the code is

String userNameVariable = "username"; // this contains
the user name

String temp2 = "WinNT://DOMAIN/username,user";
String temp1 = "WinNT://"+ Environment.UserDomainName
+ "/" + userNameVariable + ",user";

Response.Write("Result of comparison of two strings is "
+ temp2.CompareTo(temp1)); //this prints a 0 on screen

DirectoryEntry userEntry = new DirectoryEntry
(temp2); //this works fine
DirectoryEntry userEntry = new DirectoryEntry(temp1); //
when this is used, the next statement crashes with the
exception given below

Response.Write("Welcome " + userEntry.Properties
["fullname"].Value);

My question is that since both temp1 and temp2 are
exactly the same strings (as proved by the strcompare
method), how come the program crashes when I use temp1 as
a parameter to Directory entry while temp2 is fine

Any help will be greatly appreciated

Thanks
Ragz
P.S. Exception details below

Exception Details:
System.Runtime.InteropServices.COMException: Unknown
error (0x80005000)

 
 
 

Problem using DirectoryEntry objects

Post by Marc Scheuner [AD MVP » Tue, 13 May 2003 16:04:31



>I am trying to retrieve the full name of a user using the
>login name. The relevant part of the code is

>String temp2 = "WinNT://DOMAIN/username,user";
>DirectoryEntry userEntry = new DirectoryEntry(temp2);

I don't think using the WinNT provider will allow you to access the
"full name" property - just another limitation of the WinNT provider.

Use the LDAP provider instead - if need be, do a directory search
first, based on the login name (which is the "samAccountName"
property), and grab the results from the directory search.

Marc

================================================================
Marc Scheuner                        May The Source Be With You!
Bern, Switzerland                         m.scheuner(at)inova.ch

 
 
 

Problem using DirectoryEntry objects

Post by Jian-Shen Lin[M » Tue, 13 May 2003 16:23:22


It seems I can't reproduce the same, I try to use the same code and works

String temp2 = "WinNT://fareast/jslin,user";
String temp1 = "WinNT://"+ Environment.UserDomainName
+ "/" + SystemInformation.UserName+ ",user";

Console.WriteLine("Result of comparison of two strings is "
+ temp2.CompareTo(temp1)); //this prints a 0 on screen

DirectoryEntry user11Entry = new DirectoryEntry (temp2); //this works fine
DirectoryEntry user12Entry = new DirectoryEntry(temp1); //

Console.WriteLine ("Welcome " + user11Entry.Properties ["fullname"].Value);
Console.WriteLine ("Welcome " + user12Entry.Properties ["fullname"].Value);

The only differen is you use userNameVariable and I use
SystemInformation.UserName instead.
Can you check the memory to ensure the string is the same?

Thanks

Jian Shen

This posting is provided "AS IS" with no warranties, and confers no rights.

 
 
 

Problem using DirectoryEntry objects

Post by ragz » Wed, 14 May 2003 01:55:58


Thanks Jian,

This works perfectly. I do not know what caused it to not
work the first time around. I guess I need to check the
strings in memory again!!

Thanks again
Ragz
P.S. Is there a snippet of code which produces same
results using LDAP instead?

Quote:>-----Original Message-----

>It seems I can't reproduce the same, I try to use the
same code and works

>String temp2 = "WinNT://fareast/jslin,user";
>String temp1 = "WinNT://"+ Environment.UserDomainName
>+ "/" + SystemInformation.UserName+ ",user";

>Console.WriteLine("Result of comparison of two strings
is "
>+ temp2.CompareTo(temp1)); //this prints a 0 on screen

>DirectoryEntry user11Entry = new DirectoryEntry

(temp2); //this works fine
Quote:>DirectoryEntry user12Entry = new DirectoryEntry
(temp1); //

>Console.WriteLine ("Welcome " + user11Entry.Properties

["fullname"].Value);
Quote:>Console.WriteLine ("Welcome " + user12Entry.Properties

["fullname"].Value);
Quote:

>The only differen is you use userNameVariable and I use
>SystemInformation.UserName instead.
>Can you check the memory to ensure the string is the
same?

>Thanks

>Jian Shen

>This posting is provided "AS IS" with no warranties, and
confers no rights.

>.

 
 
 

Problem using DirectoryEntry objects

Post by ragz » Wed, 14 May 2003 01:57:17


Thanks Marc for your reply,

Unfortunately I am not sure how to achieve the same
results with LDAP. Is there a code snippet or a link you
can provide which can produce the same results?

Thanks again
Ragz

>-----Original Message-----
>On Fri, 9 May 2003 09:49:08 -0700, "Ragz"

>>I am trying to retrieve the full name of a user using
the
>>login name. The relevant part of the code is

>>String temp2 = "WinNT://DOMAIN/username,user";
>>DirectoryEntry userEntry = new DirectoryEntry(temp2);

>I don't think using the WinNT provider will allow you to
access the
>"full name" property - just another limitation of the
WinNT provider.

>Use the LDAP provider instead - if need be, do a
directory search
>first, based on the login name (which is

the "samAccountName"

- Show quoted text -

Quote:>property), and grab the results from the directory
search.

>Marc

>=========================================================
=======
>Marc Scheuner                        May The Source Be
With You!
>Bern, Switzerland                         m.scheuner(at)
inova.ch
>.

 
 
 

Problem using DirectoryEntry objects

Post by Marc Scheuner [AD MVP » Wed, 14 May 2003 16:42:44


Quote:>Unfortunately I am not sure how to achieve the same
>results with LDAP. Is there a code snippet or a link you
>can provide which can produce the same results?

public string GetUserFullName(string aBasePath, string aUserLogonName)
{
  // create a directory entry for the base path from which to search
  DirectoryEntry oBaseEntry = new DirectoryEntry(aBasePath);

  // create a directory searcher
  DirectorySearcher oDS = new DirectorySearcher(oBaseEntry);
  oDS.SearchScope = SearchScope.Subtree;
  oDS.Filter = "(samAccountName = " + aUserLogonName + ")";

  // set up what attributes to load
  oDS.PropertiesToLoad.Add("distinguishedName");
  oDS.PropertiesToLoad.Add("displayName");

  // search for the one entry
  SearchResult oSR = oDS.FindOne();

  // did we even find any entry with that SAMAccountName?  
  if (oSR != null)
  {
    // does that entry have a value set for "displayName" ??
    if (oSR.Properties["displayName"] != null)
    {
       // displayName is a multi-valued field - gimme the first string
       return oSR.Properties["displayName"][0].ToString();
    }
    else
    {
       // we found the user, but the attribute has no value assigned
       return "(no value set)";
    }
  }
  else
  {
    // we didn't have any luck finding that user
    return string.Empty;
  }

Quote:}

Note: there's no "fullName" attribute in AD - there's a ton of names,
"givenName" (first name), "Surname" (family name), "displayName"
(which probably comes closest to what you want - by default, this is
set to surname + givenName - or the other way around, it's
configurable).......

Hope this helps
Marc

================================================================
Marc Scheuner                        May The Source Be With You!
Bern, Switzerland                         m.scheuner(at)inova.ch

 
 
 

Problem using DirectoryEntry objects

Post by ragz » Sat, 17 May 2003 02:21:54


Thanks for taking time for this detailed reply. I really
appreciate it

Again, I do not know what the aBasePath would be in the
following example. Please excuse my ignorance in this
matter. I am specifically looking at the string which
goes like "LDAP://....."
Is there anyway I can find my LDAP server name?
I am not sure what dc=, ou= mean. Is there a link you can
provide to me which can explain this a little bit?

Thanks
Ragz

Quote:>-----Original Message-----
>>Unfortunately I am not sure how to achieve the same
>>results with LDAP. Is there a code snippet or a link
you
>>can provide which can produce the same results?

>public string GetUserFullName(string aBasePath, string
aUserLogonName)
>{
>  // create a directory entry for the base path from
which to search
>  DirectoryEntry oBaseEntry = new DirectoryEntry
(aBasePath);

>  // create a directory searcher
>  DirectorySearcher oDS = new DirectorySearcher
(oBaseEntry);
>  oDS.SearchScope = SearchScope.Subtree;
>  oDS.Filter = "(samAccountName = " + aUserLogonName
+ ")";

>  // set up what attributes to load
>  oDS.PropertiesToLoad.Add("distinguishedName");
>  oDS.PropertiesToLoad.Add("displayName");

>  // search for the one entry
>  SearchResult oSR = oDS.FindOne();

>  // did we even find any entry with that
SAMAccountName?  
>  if (oSR != null)
>  {
>    // does that entry have a value set

for "displayName" ??

- Show quoted text -

Quote:>    if (oSR.Properties["displayName"] != null)
>    {
>       // displayName is a multi-valued field - gimme
the first string
>       return oSR.Properties["displayName"][0].ToString
();
>    }
>    else
>    {
>       // we found the user, but the attribute has no
value assigned
>       return "(no value set)";
>    }
>  }
>  else
>  {
>    // we didn't have any luck finding that user
>    return string.Empty;
>  }
>}

>Note: there's no "fullName" attribute in AD - there's a
ton of names,
>"givenName" (first name), "Surname" (family

name), "displayName"

- Show quoted text -

Quote:>(which probably comes closest to what you want - by
default, this is
>set to surname + givenName - or the other way around,
it's
>configurable).......

>Hope this helps
>Marc

>=========================================================
=======
>Marc Scheuner                        May The Source Be
With You!
>Bern, Switzerland                         m.scheuner(at)
inova.ch
>.

 
 
 

Problem using DirectoryEntry objects

Post by Marc Scheuner [AD MVP » Sat, 17 May 2003 15:29:24


Quote:>Again, I do not know what the aBasePath would be in the
>following example. Please excuse my ignorance in this
>matter. I am specifically looking at the string which
>goes like "LDAP://....."
>Is there anyway I can find my LDAP server name?

Yes - you can bind to the "LDAP://RootDSE" path, and query the
"defaultNamingContext" property:

DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");
string strDefaultNaming =
deRoot.Properties["defaultNamingContext"][0].ToString();

Quote:>I am not sure what dc=, ou= mean. Is there a link you can
>provide to me which can explain this a little bit?

OU stands for "Organizational Unit" which is the way Active Directory
organizes and creates a hierarchy of objects. Rather than just having
a flat list of users and groups as WinNT 4 did, you have a hierarchy
of objects, organized into organizational units (as containers).

dc= stands for "domain component", AFAIK, and it denotes the "domain
part" of your LDAP path - your default naming context should return
something that contains one or multiple dc= sections.

Based on where a user is located in the AD object hierarchy, you will
have to specify his LDAP path. Let's assume "John Doe" is in the
Research OU for the Boston location (which is an OU, too), which in
turn is contained in the NorthAmerica OU of the BigCompany, then his
LDAP path would be:

"LDAP://cn=John Doe,
OU=Research,OU=Boston,OU=NorthAmerica,dc=BigCompany,dc=com"

It really almost reads like a directory entry, right? - well, IT IS !
:-)

Does that help clarify things??

Cheers!
Marc

================================================================
Marc Scheuner                        May The Source Be With You!
Bern, Switzerland                         m.scheuner(at)inova.ch

 
 
 

1. How to set properties using DirectoryEntry object

I am using a DirectoryEntry object to try to create a virtual directory, but
I need to set properties as well as invoke methods on the IIS provider, how
do I do this?

obDirEntry = new DirectoryEntry("IIS://" + strServer + "/W3svc/1/Root");

// create new dir

oWebDir = obDirEntry.Invoke("Create", "IIsWebVirtualDir", "NewVDir") as
DirectoryEntry;

oWebDir.Properties["Path"] = "c:\\inetpub\\wwwroot\\NewContent";

oWebDir.Properties["AccessRead"] = "True";

oWebDir.Properties["AccessScript"] = "True";

oWebDir.Invoke("AppCreacte2", "1");

oWebDir.Properties["AppFriendlyName"] = "NewApp";

oWebDir.Invoke("SetInfo");

2. SMS reports

3. ADSI problems using SetPassword with DirectoryEntry.Invoke

4. Install-Linux-Firewall

5. ADSI WinNT C# DirectoryEntry object can't delete computers

6. txt to pdf?

7. Using DirectoryEntry.NativeObject.SetPassword()

8. overwriting html with a form

9. Using samAccountName in LDAP path with DirectoryEntry

10. Problem in creating objects using ActiveDs Type Library

11. Problem etrieving Deleted Objects From Active Directory Using ADSI

12. DirectoryEntry RefreshCache fails due to access problem - schema retrieval on 3rd party ldap server

13. ASP.net: problem getting DirectoryEntry