Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#donds'从Active Directory获取全名;部署后无法工作_C#_Asp.net_Iis_Active Directory_Ldap - Fatal编程技术网

使用C#donds'从Active Directory获取全名;部署后无法工作

使用C#donds'从Active Directory获取全名;部署后无法工作,c#,asp.net,iis,active-directory,ldap,C#,Asp.net,Iis,Active Directory,Ldap,我正在使用以下代码从服务器获取用户的全名: public string getUserName(int empID) { DirectoryEntry objDirectoryEntry = new DirectoryEntry("LDAP://SOMEDOMAIN.com"); objDirectoryEntry.AuthenticationType = AuthenticationTypes.Secure;

我正在使用以下代码从服务器获取用户的全名:

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_建议的那样,我尝试使用以下代码:

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配置是否有问题(我怀疑是这样)?

如果您使用的是.NET 3.5及更高版本,则应检查
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。请在此处阅读所有相关内容:

基本上,您可以定义域上下文并在AD中轻松找到用户和/或组:

// 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, Environment.UserName);

    /* 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)
        string fullName = user.DisplayName; 

        // do something here....        
    }
}

新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易

学习记录您正在做的事情并调试您自己的代码。
WinNT://
提供程序用于您的本地计算机用户存储-它不是Active Directory-基于网络的存储!为此,您需要使用
LDAP://
提供程序@谢谢。。我尝试过使用LDAP。结果是一样的。在本地工作很好。在服务器上不工作。请检查更新的问题。该属性名为
displayName
(不是
displayName
)-是的,我认为它实际上区分大小写)-请尝试使用正确的拼写-这是否返回值?或者可能那个用户帐户上没有设置那个值……我试过使用它。同样的事情也发生在这里。它在本地运行良好。但在部署之后,它就不起作用了。但是,如果我在调试模式下运行项目并打开断点。它很好用。我怀疑IIS有问题。你觉得@marc_s怎么样?