Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/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# 4.0 仅使用用户名从广告中获取用户信息_C# 4.0_Authentication_Active Directory - Fatal编程技术网

C# 4.0 仅使用用户名从广告中获取用户信息

C# 4.0 仅使用用户名从广告中获取用户信息,c#-4.0,authentication,active-directory,C# 4.0,Authentication,Active Directory,我有以下代码 private static string ADValidateUser(string LDAP_URL, string username, string password) { System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(LDAP_URL, username, password); Direc

我有以下代码

    private static string ADValidateUser(string LDAP_URL, string username, string password)
    {
          System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(LDAP_URL, username, password);
        DirectorySearcher mySearcher = new DirectorySearcher(myDE);

        mySearcher.Filter = ("sAMAccountName=" + username);

        try
        {
            SearchResult myresult = mySearcher.FindOne();
            string strName = myresult.GetDirectoryEntry().Properties["displayname"].Value.ToString();
            string strCompany = myresult.GetDirectoryEntry().Properties["company"].Value.ToString();
            return ("TRUE|" + (strName + ("|" + strCompany)).ToUpper());
        }
        catch (Exception ex)
        {
            return "FALSE|None";
        }
    }
我需要修改此方法以获取指定的用户信息,而不使用密码,而只使用用户名


谢谢。

这会起作用,但假设此进程以有权访问LDAP\u URL中指定目录的用户身份运行

本质上,更改是调用DirectoryEntry。。。构造函数,并省略使用当前用户的用户名和密码

private static string ADValidateUser(string LDAP_URL, string username)
{
      System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(LDAP_URL);
    DirectorySearcher mySearcher = new DirectorySearcher(myDE);

    mySearcher.Filter = ("sAMAccountName=" + username);

    try
    {
        SearchResult myresult = mySearcher.FindOne();
        string strName = myresult.GetDirectoryEntry().Properties["displayname"].Value.ToString();
        string strCompany = myresult.GetDirectoryEntry().Properties["company"].Value.ToString();
        return ("TRUE|" + (strName + ("|" + strCompany)).ToUpper());
    }
    catch (Exception ex)
    {
        return "FALSE|None";
    }
}