开发者

Getting manager email id from active directory

开发者 https://www.devze.com 2023-03-02 16:17 出处:网络
How to get user manager email id from active directory? 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 e

How to get user manager email id from active directory? 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.

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. If you have any troubles with connection to AD, you can try to add AD server name in PrincipalContext constructor.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消