C# 从客户端调用时,DirectoryEntry无法获取全名
我有以下功能:C# 从客户端调用时,DirectoryEntry无法获取全名,c#,asp.net,active-directory,C#,Asp.net,Active Directory,我有以下功能: public string GetADDisplayName(string strLogin) { int length = strLogin.IndexOf('\\'); if (length == -1) length = strLogin.IndexOf('@'); string str1; string str2; if (length != -1) { str1 = strLogin.Sub
public string GetADDisplayName(string strLogin)
{
int length = strLogin.IndexOf('\\');
if (length == -1)
length = strLogin.IndexOf('@');
string str1;
string str2;
if (length != -1)
{
str1 = strLogin.Substring(0, length);
str2 = strLogin.Substring(length + 1);
}
else
{
str1 = Environment.MachineName;
str2 = strLogin;
}
string str3;
try
{
str3 = new DirectoryEntry("WinNT://" + str1 + "/" + str2).Properties["FullName"].Value.ToString();
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('GetADDisplayName ERROR: " + ex.ToString() + "');", true);
str3 = "";
}
return str3;
}
我传递给函数的值是HttpContext.Current.User.Identity.Name.Trim()
当我在VisualStudio中调试/F5时,它会显示全名
当我部署到web服务器并从web服务器本身中访问它时,它会显示全名
但当我从客户端访问web服务器时,它无法显示全名,这是为什么?尝试以下方法:
using System;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using (var principalContext = new PrincipalContext(ContextType.Domain))
{
using (var userPrincipal = new UserPrincipal(principalContext))
{
userPrincipal.SamAccountName = 'userdomain name'; // -> ex. jtabuloc
using (var principalSearcher = new PrincipalSearcher())
{
principalSearcher.QueryFilter = userPrincipal;
var principal = principalSearcher.FindOne();
if (principal != null)
{
var directoryEntry = (DirectoryEntry)principal.GetUnderlyingObject();
// You can examine directoryEntry if key exist and retrieve values
var Name = directoryEntry["name"][0] as string;
var FullName = directoryEntry["FullName"][0] as string;
var Email = directoryEntry["mail"][0] as string;
var Title = directoryEntry["title"][0] as string;
}
}
}
}
尝试使用
AccountManagement
获取FullName
:
using System.DirectoryServices.AccountManagement;
UserPrincipal userPrincipal = UserPrincipal.Current;
String name = userPrincipal.DisplayName;
这可以进一步简化。由于您是通过
SamAccountName
搜索单个用户,因此实际上不需要使用PrincipalSearcher
。您可以将第5-9行合并为一行<代码>使用(UserPrincipal principal=UserPrincipal.FindByIdentity(principalContext,IdentityType.SamAccountName,username)此外,如果任何目录对象的值为空,则您的代码将失败。在获取任何字段的值之前,您需要检查它是否有值。通过客户端从web服务器访问时出错。异常详细信息:System.DirectoryServices.DirectoryServicesCOMException:发生操作错误。
如果从在web服务器内。源错误:在执行当前web请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。
[DirectoryServicesCOMException(0x80072020):发生操作错误。]System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)+377678 System.DirectoryServices.DirectoryEntry.Bind()+36 System.DirectoryServices.DirectoryEntry.get_AdsObject()+31 System.DirectoryServices.PropertyValueCollection.PopulateList()+26 System.DirectoryServices.PropertyValueCollection(DirectoryEntry条目,字符串propertyName)+49 System.DirectoryServices.PropertyCollection.get_项(字符串propertyName)+150 System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()+1114 System.DirectoryServices.AccountManagement.PrincipalContext.DodoDomainInit()+37 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()+112 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()+31 System.DirectoryServices.AccountManagement.Principal.FindByIdentity with TypeHelper(PrincipalContext上下文,类型principalType,可空`1 identityType,字符串identityValue,DateTime refDate)+14