[英]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).
这是Outlook在网络上搜索用户时获得的信息(Outlook用来填充联系人卡的信息)。 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?
我用什么物体来得到它?
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. 要从Active Directory中读取,所需的类位于System.DirectoryServices.dll中。 The important ones are DirectorySearcher and DirectoryEntry.
重要的是DirectorySearcher和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#
查看此其他问题的前两个答案,以获取更多代码: 如何在C#中获取当前用户的Active Directory详细信息
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. 更新:要获取当前用户的信息,请使用其登录名,然后在Active Directory中搜索具有相同用户ID的用户。 Something like this:
像这样的东西:
adSearch.Filter = "(sAMAccountName=" + Environment.UserName + ")";
adSearch.Filter =“(sAMAccountName =” + Environment.UserName +“)”;
(In ASP.Net you would get the user name elsewhere.) (在ASP.Net中,您将在其他位置获得用户名。)
If you're using Active Directory, then you can query that information from there, given the domain and username. 如果您使用的是Active Directory,则可以在给定域和用户名的情况下从那里查询该信息。 It's basically an LDAP store, but there's tonnes of info on the web for how to implement it.
它基本上是一个LDAP存储,但是在网络上有很多关于如何实现它的信息。
Here's another question that specifically asks how to retrieve this information from Active Directory: 这是另一个问题,专门询问如何从Active Directory中检索此信息:
How to get the current user's Active Directory details in C# 如何在C#中获取当前用户的Active Directory详细信息
In a desktop app, to get the current logged-on user's Windows username, you can use Environment.UserName
. 在桌面应用程序中,要获取当前登录用户的Windows用户名,可以使用
Environment.UserName
。
One additional tip to go along with the answers David and Neil Barnwell provided: David和Neil Barnwell提供的答案还有另外一个提示:
You can get their ID using HttpContext.Current.User.Identity.Name
. 您可以使用
HttpContext.Current.User.Identity.Name
获取其ID。
If you want use this from an assembly or some other back end code, make sure you add using System.Web
. 如果要从程序集或其他后端代码中使用它,请确保
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: 要回答您的问题“是否有办法获取当前的域控制器...”,对我有所帮助的一种方法是使用
nltest
命令[1],如下所示:
C:\>nltest /dsgetdc:yourdomain.com
It should output all kinds of useful stuff. 它应该输出各种有用的东西。
[1] http://support.microsoft.com/kb/247811 [1] http://support.microsoft.com/kb/247811
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.