我有以下功能:
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()
。
当我在visual studio中调试/ F5时,它会显示FullName。
当我部署到Web服务器并从Web服务器本身访问它时,它会显示FullName。
但是当我从客户端访问Web服务器时,它未能显示FullName,为什么会这样?
答案 0 :(得分:0)
尝试这种方法:
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;
}
}
}
}
答案 1 :(得分:0)
尝试使用FullName
获取AccountManagement
:
using System.DirectoryServices.AccountManagement;
UserPrincipal userPrincipal = UserPrincipal.Current;
String name = userPrincipal.DisplayName;