current user in Active Directory

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
How do I find the user object for the current user in Active Directory i.e.
the user running my program ?
Regards
Michael
 
Michael,

if it is the same for you, the user running your program is at a windowsform
program

Environment.username

I hope this helps,

Cor
 
The user running ASP.NET (When running with impersonation)
is System.Security.Principal.WindowsIdentity.GetCurrent().Name;

Now you have them both :-)
 
Rainer,
Thanks - that will get me the user name - how do I then point that at Active
Directory with 100% certainty ?
Regards
Michael
 
Oke ones you got the current user you will have to connect to Active
directory and get the properties you want. Here is some code that will do
just that.

/// <summary>
/// Connects to the Active Directory and retrieves the value of the
property asked for.
/// </summary>
/// <param name="PropertyName">Name of the property to retrieve the value
for.</param>
/// <returns>Value of the proeprty, string.empty if property not
found.</returns>
internal string getADirectoryValue(string PropertyName)
{
try
{
DirectoryEntry directoryEntry = new DirectoryEntry(@"LDAP://" +
_aDDomain, _aDLogonUser,_aDLogonUserPassword );
DirectorySearcher directorySearcher = new
DirectorySearcher(directoryEntry);
SearchResult searchResult;
directorySearcher.Filter = "(anr=" + _userName + ")";
searchResult = directorySearcher.FindOne();
DirectoryEntry foundEntry = searchResult.GetDirectoryEntry();
return foundEntry.Properties[PropertyName].Value.ToString();
}
catch(Exception ex)
{
System.Diagnostics.Trace.WriteLine(string.Concat(
"Active Directory Property with name : ",
PropertyName,
" Not found or Active Directory logon failed:",
ex.ToString()));
return string.Empty;
}
}
--
Rainier van Slingerlandt
www.slingerlandt.com

Please hit the Yes button If my effort is helpfull.
 
Back
Top