Getting AD Path of Current User

  • Thread starter Thread starter Andy D
  • Start date Start date
A

Andy D

Hi all,

I am having a little difficulty with the System.DirectoryServices namespace
and Windows Principals in a C# project I am working on.

What I would like to do is this:-

1. Get the current principal or Credential (or whatever is needed).
2. Get the fully qualified Active directory path (i.e.
CN=ANDY,DC=MYDOMAIN,DN=DEV) so that I can create a DirectoryEntry object.
3. Enum the groups from the DirectoryEntry object created.

So far all I got was the domain name (in the old WindowsNT format - i.e.
MYDOMAIN) and the user name (i.e. ANDY). I can also get the Windows
Principal to get that as MYDOMAIN\ANDY but I cant seem to create a
DirectoryEntry object from that name.

Can anyone point me in the right direction?

Thanks in advance,
Andy D
 
What I would like to do is this:-
1. Get the current principal or Credential (or whatever is needed).
2. Get the fully qualified Active directory path (i.e.
CN=ANDY,DC=MYDOMAIN,DN=DEV) so that I can create a DirectoryEntry object.
3. Enum the groups from the DirectoryEntry object created.

So far all I got was the domain name (in the old WindowsNT format - i.e.
MYDOMAIN) and the user name (i.e. ANDY). I can also get the Windows
Principal to get that as MYDOMAIN\ANDY but I cant seem to create a
DirectoryEntry object from that name.

Ahem - yes, the "logon" name that you get from Environment.Username et
al. is the "old style" NT user name - not the LDAP info you need.

There's a number of things you could do:

1) Create a DirectorySearcher for your e.g. domain
(dc=mycompany,dc=com) and search for your user (what you get is the
sAMAccountName) - this works, but might be a bit too complicated /
time-consuming.

2) You could use the ActiveDs IADsSystemInfo interface to get the
currently logged on user (his relative DN)

3) You could use the ActiveDs IADsNameTranslate interface to translate
your "NT user name" into a LDAP relative DN

I've implemented #2 and #3 in this code snippet here - you need to add
a reference to the COM ActiveDs Type Library in your project, in order
for this to work. Hope it helps - enjoy!

Marc



using System;
using ActiveDs;

namespace GetCurrentUser
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Current user (Environment)
is: " + Environment.UserName);

ActiveDs.IADsADSystemInfo oSysInfo = new
ActiveDs.ADSystemInfoClass();
Console.WriteLine("Current user (ActiveDs) is:
" + oSysInfo.UserName);

ActiveDs.IADsNameTranslate oNameTranslate =
new ActiveDs.NameTranslateClass();

oNameTranslate.Init((int)ActiveDs.ADS_NAME_INITTYPE_ENUM.ADS_NAME_INITTYPE_DOMAIN,
"INOVA");

oNameTranslate.Set((int)ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4,
@"INOVA\" + Environment.UserName);
string sTemp =
oNameTranslate.Get((int)ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_1779);

Console.WriteLine("Current user
(NameTranslate) is: " + sTemp);
}
}
}

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