【发布时间】:2015-11-29 15:07:00
【问题描述】:
我正在使用以下代码从服务器获取用户的全名:
public string getUserName(int empID)
{
DirectoryEntry objDirectoryEntry = new DirectoryEntry("LDAP://SOMEDOMAIN.com");
objDirectoryEntry.AuthenticationType = AuthenticationTypes.Secure;
DirectorySearcher objDirectorySearch = new DirectorySearcher(objDirectoryEntry);
objDirectorySearch.Filter = "(SAMAccountName=" + empID + ")";
objDirectorySearch.PropertiesToLoad.Add("displayName");
SearchResult objSearchResult = objDirectorySearch.FindOne();
if (objSearchResult != null)
return Convert.ToString(objSearchResult.Properties["displayname"][0]);
else
return "User not found";
}
这在我的本地机器上运行良好。
但是,将其部署到服务器后,它根本不起作用。
这将返回 NULL。
正如@marc_s 建议的那样,我尝试使用以下代码:
public string getUserStackExchange(string empID)
{
string fullName = empID;
// set up domain context using the default domain you're currently logged in
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, empID);
/* or if you're interested in the *currently logged in* user,
then you could also use:
UserPrincipal user = UserPrincipal.Current;
*/
if (user != null)
{
// get the "DisplayName" property ("Fullname" is WinNT specific)
fullName = user.DisplayName;
// do something here....
}
}
return fullName;
}
同样的结果。在本地工作得很好。但是部署后就不行了。 但是,如果我在调试模式下运行项目并打开断点,它会完美地获取数据。
代码或 IIS 配置有问题吗(我怀疑这个)?
【问题讨论】:
-
学习记录你正在做的事情并调试你自己的代码。
-
WinNT://提供程序用于您的本地计算机用户存储 - 它不是 Active Directory - 基于网络的存储!您需要为此使用LDAP://提供程序! -
@marc_s 谢谢.. 我试过使用 LDAP。结果是一样的。在本地工作正常。在服务器上不起作用。请检查更新的问题。
-
该属性称为
displayName(不是displayname- 是的,我认为它实际上是区分大小写) - 尝试使用正确的拼写 - 是否返回值?或者也许该值只是没有在该用户帐户上设置...
标签: c# asp.net iis active-directory ldap