Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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# 正在从active directory获取管理员电子邮件id_C#_Active Directory - Fatal编程技术网

C# 正在从active directory获取管理员电子邮件id

C# 正在从active directory获取管理员电子邮件id,c#,active-directory,C#,Active Directory,如何从active directory获取用户管理器电子邮件id?我已经编写了代码,可以根据userid获取用户的名字、姓氏、电子邮件id和他的经理姓名,但我想获取经理电子邮件id和他的经理姓名 谁能帮我弄到这个吗?这是我的密码: protected void ddlAdsuser_SelectedIndexChanged(object sender, EventArgs e) { DirectoryEntry root = new DirectoryEntry("LDAP://Root

如何从active directory获取用户管理器电子邮件id?我已经编写了代码,可以根据userid获取用户的名字、姓氏、电子邮件id和他的经理姓名,但我想获取经理电子邮件id和他的经理姓名

谁能帮我弄到这个吗?这是我的密码:

protected void ddlAdsuser_SelectedIndexChanged(object sender, EventArgs e)
{
    DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
    string myDomain = root.Properties["defaultNamingContext"].Value.ToString();
    DirectoryEntry domain = new DirectoryEntry("LDAP://" + myDomain);
    DirectorySearcher dsUsers = new DirectorySearcher(domain);
    dsUsers.Filter = "(userPrincipalName=" + ddlAdsuser.Text + ")";
    foreach (SearchResult sResultSet in dsUsers.FindAll())
        {
            lblfname.Text = GetProperty(sResultSet, "givenName");
            lbllname.Text = GetProperty(sResultSet, "sn");
            lblemail.Text = GetProperty(sResultSet, "mail");

            string Manager = string.Empty;
            Manager = GetProperty(sResultSet, "manager");
            if (Manager != "")
            {
                if (Manager.Contains("CN="))
                {
                    int Length = Manager.IndexOf(',');
                    Manager = Manager.Substring(3, Length - 3);
                }
                else
                {
                    Manager = string.Empty;
                }
            }            
            lblManagerID.Text = Manager;  //Here displaying the manager name.
        }     
}

public static string GetProperty(SearchResult searchResult, string PropertyName)
{
    if (searchResult.Properties.Contains(PropertyName))
    {
        return searchResult.Properties[PropertyName][0].ToString();
    }
    else
    {
        return string.Empty;
    }
}

只需再次搜索经理


请注意,您构建查询筛选器的方法有缺陷,您需要转义一些字符(尤其是
引号),以避免根据用户输入中断查询。

代码简单,工作正常:

    public static string GetEmail(string userId)
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userId);
        return user.EmailAddress;
    }
必须添加程序集System.DirectoryServices.AccountManagement.dll。
如果您在连接到AD时遇到任何问题,您可以尝试在PrincipalContext构造函数中添加AD服务器名称。

Lucero->我不明白。请详细说明。什么部分?用您构建过滤器的方式搜索问题的管理者?使用管理者的CN,您可以轻松地从active directory中检索它。
DirectorySearcher objDirSearch = new DirectorySearcher(SearchRoot);
DirectoryEntry dentUser = null;
string pstrFieldName, pstrValue;

pstrFieldName = "company";
pstrValue = "12345"; //Employee number

/*setting the filter as per the employee number*/
objDirSearch.Filter = "(&(objectClass=user)(" + pstrFieldName + "=" + pstrValue + "))";

SearchResult objResults = objDirectorySearch.FindOne();

dentUser = new DirectoryEntry(objResults.Path);}

string strManager = dentUser.Properties["manager"].Value.ToString();

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, strManager);

string strManagerMailID = user.EmailAddress;