Link to home
Start Free TrialLog in
Avatar of krypto2000
krypto2000

asked on

Get ActiveDirectory "department" of connected user using ASP.NET/C#

Hello,

I want to get the department name of the user that is connected to the web page.

I'm under a Win2K domain and I need to get the username of the user and after make an LDAP query to get the department name.

That's all ;-)
Avatar of ihenry
ihenry


The following PAQ has C# code that sending a query to AD given a domain user id then retrieve the user detail information.
https://www.experts-exchange.com/questions/21291754/Attempting-to-retrieve-user-info-from-Active-Directory.html

To get department name of the user you can add these lines

          String departmentName = "";
          if ( sr.Properties.Contains("department") )
               firstName = sr.Properties["department"][0];

To get the domain user id depends on how the user gets connected to your web app? for intranet application (Windows authentication) or with impersonation you can simply do this.

String domainId = WindowsIdentity.GetCurrent.Name;
Avatar of krypto2000

ASKER

Okay that's fine but there is many different code and it's hard for me to understand the logic...

I'm new to dotNet you know and many think is really different... can you give me a parts of working code ?

this is where I am :
--------------------------------------------------------------------------------------------------------------------------------------

string userName = getUserName(WindowsIdentity.GetCurrent().Name);

DirectoryEntry de = new DirectoryEntry("LDAP://rootDSE");

string DomainPath = de.Properties["DefaultNamingContext"][0].ToString();

DirectoryEntry root = new DirectoryEntry(DomainPath);
DirectorySearcher ds = new DirectorySearcher(root);
   ds.Filter = "(&(objectCategory=Person)(SAMAccountName=" + userName + ")";
   ds.PropertiesToLoad.Add("department");

   ds.FindAll(); <-- unspecified error !!!!

--------------------------------------------------------------------------------------------------------------------------------------
STACK TRACE
--------------------------------------------------------------------------------------------------------------------------------------
[COMException (0x80004005): Erreur non spécifiée]
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +514
   System.DirectoryServices.DirectoryEntry.Bind() +10
   System.DirectoryServices.DirectoryEntry.get_AdsObject()
   System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
   System.DirectoryServices.DirectorySearcher.FindAll()
   ldapMenu.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\ldapmenu\main.aspx.cs:47
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()

 
ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay thank you I think your code is great but I'm so new to dotNet...
and I have some stupid problems due to transition...

I have create the two classes, but when after in my main form I have problem to use this...

Here is my code :

userInfo ui = new UserInfo(domainUserID);
ui = GetUser(domainUserID); <-- can't find GetUser method

The name 'GetUser' does not exist in the class or namespace 'ldapMenu.WebForm1'

What I have forgot ??

thx
Now that's work !!!

But i have to remove the CN=Users in the search location path, otherwise the search result collection.count equals to 0
and I have paste the function in the main program because i can't access it if it's in another classes...

I would try to make a web services that return user informations from LDAP..

thanks for all and if u can, explain me for what i can't access the function.

many thanks !!!

Ok, my apologize for replying late. I didn't know you actually have another q, because as usual I delete almost all "accepted answer" emails from my mailbox and never return back to the q.

q: why you can't access the function?
That could be caused of some reasons.
1. You didn't initialize the class into an object instance.
2. The class created in a different namespace name, therefore you need to import the namespace in the calling class.
3. Not very sure, maybe you can post here some of your code so I can see what exactly the problem is.