现在的位置: 首页 > 综合 > 正文

C#AdServer相关操作

2013年03月15日 ⁄ 综合 ⁄ 共 2792字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.