C#串連AD

來源:互聯網
上載者:User

using System;
using System.Xml;
using System.Collections;
using System.DirectoryServices;
using System.Windows.Forms;

namespace LegendNet.Common.Ldap
{
 /// <summary>
 /// legendAD 的摘要說明。
 /// </summary>
 public class legendAD
 {
  public legendAD()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }
  private string adServer;
  private int adPort;
  private string dn;
  //AD伺服器的IP地址
  public string AdServer
  {
   get{return adServer;}
   set{adServer=value;}
  }
  //AD伺服器的port
  public int AdPort
  {
   get{return adPort;}
   set{adPort=value;}
  }
  //AD伺服器的Dn串
  public string Dn
  {
   get{return dn;}
   set{dn=value;}
  }
  public legendAD(string filePath)
  {
   this.CheckConfig(filePath);
  }
  /// <summary>
  private void CheckConfig(string filePath)
  {
   try
   {
    XmlDocument _xd=new XmlDocument();
    _xd.Load(filePath);
    XmlElement root=_xd.DocumentElement;
    XmlNodeList _xnl=root.GetElementsByTagName("ad_cfg");
    IEnumerator ienum = _xnl.GetEnumerator();
    ienum.MoveNext();
    ienum=((XmlNode)ienum.Current).ChildNodes.GetEnumerator();
    while(ienum.MoveNext())
    { 
     XmlNode title = (XmlNode) ienum.Current;
     switch(title.Name)
     {
      case "ad_server":
      {
       this.adServer=title.InnerText;
       break;
      }
      case "ad_port":
      {
       this.adPort=int.Parse(title.InnerText);
       break;
      }
      case "dn":
      {
       this.dn=title.InnerText;
       break;
      }
     }
    }
   }
   catch(Exception e)
   {
    throw new Exception("載入AD設定檔出錯,錯誤 "+e.Message);
   }
  }

  /// <summary>
  /// 登陸,並返回使用者資訊Entry
  /// </summary>
  ///

  //如果需要顯示使用者的詳細資料,用此資訊
  public  DirectoryEntry  Login(string userName,string password)
  {
   try
   {
    string path="LDAP://"+this.adServer+":"+this.adPort+"/"+this.dn;
    DirectoryEntry de=new DirectoryEntry(path,userName,password); 
    de.RefreshCache();
    return de;
   }
   catch(Exception e)
   { 
    MessageBox.Show(e.Message);
    return null;
   }
  }
  public bool CheckUser(string userName,string password)
  {
   try
   {
    string path="
LDAP://"+this.adServer+":"+this.adPort+"/"+this.dn;
    DirectoryEntry de=new DirectoryEntry(path,userName,password); 
    de.RefreshCache();
    return true;
   }
   catch(Exception e)
   { 
    MessageBox.Show(e.Message);
    return false;
   }
  }

  //查詢AD使用者的屬性
  public ArrayList searchinfo(DirectoryEntry de)
  {
   ArrayList ls=new ArrayList();
   
   try
   {
    DirectorySearcher sear=new DirectorySearcher();
    sear.SearchRoot=de;
    sear.SearchScope=SearchScope.Subtree;
    //範圍、類型、帳號
    sear.Filter="(&(objectCategory=person)(objectClass=user)(samaccountname="+de.Username+"))";
    //PropertiesToLoad.Add方法,用於設定要顯示的使用者資訊。
    sear.PropertiesToLoad.Clear();
    SearchResultCollection rs=sear.FindAll();
    foreach(SearchResult r in rs)
    {
     ResultPropertyCollection rprops=r.Properties;
     string prop=null;
     foreach(string name in rprops.PropertyNames)
     {
      foreach(object vl in rprops[name])
      {
       prop=name+":"+vl.ToString();
       ls.Add(prop);
      }
     }
    }
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   return ls;
  }
 }
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.