スポンサーリンク
スポンサーリンク

ActiveDirectory からメールアドレスを取得する。ドメイン参加していないPCから、ActiveDirectory内を検索する場合、ログインが必要。

ActiveDirectory

using System.DirectoryServices;
using ActiveDs;

 

try
{
    // ActiveDirectoryのIPアドレス:192.168.1.100
    // ドメイン:pr.local
    // pr.localドメインに登録されているユーザー:testuser
    // testuserのパスワード:testpd

    string adPath = "LDAP://192.168.1.100/DC=pr,DC=local";
    string userDN = "testuser";
    string sPass = "testpd";
    DirectoryEntry de = new DirectoryEntry(adPath, userDN, sPass, AuthenticationTypes.None);
    DirectorySearcher schr = new DirectorySearcher(de);
    schr.SizeLimit = 2147483647;
    schr.PageSize = 1000;
    schr.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", strUserName);
    SearchResult sr = schr.FindOne();

    if (sr == null)
    {
        throw new System.ArgumentException("Not found user " + strUserName + " using Active Directory.", "original");
    }

    DirectoryEntry userEntry = sr.GetDirectoryEntry();

    string strMail = (string)userEntry.Properties["mail"].Value;
    if (strMail == "")
    {
        throw new ArgumentException("Nothing Mail Address to " + strUserName, "original");
    }

    return strMail;

}
catch (ArgumentException ex)
{
    throw ex;
}

コメント

タイトルとURLをコピーしました