简体   繁体   中英

How do you get user info from a company's domain/network settings in C#?

I was told that user info like name, address, phone, etc are stored on the network (obviously only if a person has given that info). This is the info that Outlook gets when searching for users on the network (the info that Outlook populates the Contact card with). This obviously works because I can search for anyone in my company's network and I get results for people all over the world that are on our network.

The program I am working on is internal to our company and one of my tasks is to pre-populate a form with that info.

My question is, how do I get this information? Where is it stored? What object do I use to get it?

I was a little general on this. 我对此有点一般。 What I really need is the current user's info ('current' being whoever is logged on to the computer and using my program). What is the best way to get it?

To read from Active Directory, the classes you need are in System.DirectoryServices.dll. The important ones are DirectorySearcher and DirectoryEntry. Take a look at the the first 2 answers to this other question to get more code: How to get the current user's Active Directory details in C#

Update: To get the current user's info, take their logon name and then do a search in Active Directory for a user with the same user ID. Something like this:

adSearch.Filter = "(sAMAccountName=" + Environment.UserName + ")";

(In ASP.Net you would get the user name elsewhere.)

If you're using Active Directory, then you can query that information from there, given the domain and username. It's basically an LDAP store, but there's tonnes of info on the web for how to implement it.

Here's another question that specifically asks how to retrieve this information from Active Directory:

How to get the current user's Active Directory details in C#

In a desktop app, to get the current logged-on user's Windows username, you can use Environment.UserName .

One additional tip to go along with the answers David and Neil Barnwell provided:

You can get their ID using HttpContext.Current.User.Identity.Name .

If you want use this from an assembly or some other back end code, make sure you add using System.Web .

To answer your question of "is there a way to get the current domain controller..." one way that has been helpful for me is to use the nltest command [1] like this:

C:\>nltest /dsgetdc:yourdomain.com

It should output all kinds of useful stuff.

[1] http://support.microsoft.com/kb/247811

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM