2011年10月7日 星期五

Authetication with Active Directory

最近公司的內部系統的登入驗證要使用LDAP對Active Directory進行驗證

主要是參考這篇,裡面的範例直接拿來使用即可
http://msdn.microsoft.com/zh-tw/library/ms180890%28VS.80%29.aspx

不過還是有些觀念稍微memo下來

1.大概了解一下什麼是Active Directory:http://zh.wikipedia.org/wiki/Active_Directory

2.MSDN中的範例中,有示範了如何抓帳號和所屬的group,若要參考有哪些attribute可抓
我是直接用Active Directory Explorer(v1.42)去看我可以抓哪些attribute,例如objectSid。
下載網址:http://technet.microsoft.com/en-us/sysinternals/bb963907.aspx

3.各個attribute的定義可參考http://msdn.microsoft.com/en-us/library/ms680982%28v=VS.85%29.aspx
4.主要是用DirectorySearcher來找AD的資料
利用Filter來搜尋資料
然後再用PropertiesToLoad.Add來設定要抓的屬性
例:

  DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(SAMAccountName=" + userName + ")";
    search.PropertiesToLoad.Add("cn");
  search.PropertiesToLoad.Add("objectSid");

  SearchResult adResult = search.FindOne();

    if (null == adResult)
       {
        return false;
       }
             
    _filterAttribute = (string)adResult.Properties["cn"][0];

沒有留言:

張貼留言