zoukankan      html  css  js  c++  java
  • C#AdServer相关操作 dodo

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

  • 相关阅读:
    LockFree的栈实现及与加锁实现的性能对比
    redis源码笔记-redis.conf
    【ASP.NET】应用程序、页面和控件的生命周期
    【ASP.NET】HTTP客户请求的数据格式说明
    【ASP.NET】页面间传值
    【ASP.NET】Page.IsPostBack 属性
    【ASP.NET】互联网HTTP连接等出错代码大全
    【经验分享】抽象类、虚函数、接口、多态 概念与关系的理解
    【架构设计】需求分析
    【经验分享】常用正则表达式收集
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/1639431.html
Copyright © 2011-2022 走看看