开发者

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开发者_如何学Go 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?

EDIT: 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜