简体   繁体   English

从活动目录获取经理 email id

[英]Getting manager email id from active directory

How to get user manager email id from active directory?如何从活动目录中获取用户管理器 email id? I have written code with which I can get user's firstname, lastname, email id and his manager name based on userid, but I want to get manager email id along with his manager name.我已经编写了代码,可以根据用户 ID 获取用户的名字、姓氏、email id 和他的经理姓名,但我想获取经理 email id 以及他的经理姓名。

Can somebody please help me how to get this?有人可以帮我如何得到这个吗? Here is my code:这是我的代码:

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;
    }
}
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; 

Simple code and working great:简单的代码和很好的工作:

    public static string GetEmail(string userId)
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userId);
        return user.EmailAddress;
    }

You must add assembly System.DirectoryServices.AccountManagement.dll.您必须添加程序集 System.DirectoryServices.AccountManagement.dll。 If you have any troubles with connection to AD, you can try to add AD server name in PrincipalContext constructor.如果您在连接 AD 时遇到任何问题,可以尝试在 PrincipalContext 构造函数中添加 AD 服务器名称。

Just do a second search for the manager.只需再次搜索经理。

Note that you way of building the query filter is buggy, you need to escape some characters (Especially the " quote) in order to avoid broken queries depending on user input.请注意,您构建查询过滤器的方式是错误的,您需要转义一些字符(特别是"引号”)以避免根据用户输入破坏查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM