C#连接AD

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

要修改Active Directory(AD)的密码,可以使用C#的System.DirectoryServices命名空间提供的类来实现。下面是一个示例代码: ```csharp using System.DirectoryServices; //指定AD域名称 string domainName = "yourdomain.com"; //指定AD域管理员账号 string adminName = "admin"; //指定AD域管理员密码 string adminPassword = "password"; //指定要修改密码的用户账号 string userName = "user"; //指定要修改的密码 string newPassword = "newpassword"; //创建AD连接 DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainName, adminName, adminPassword); //指定要修改密码的用户账号路径 DirectorySearcher searcher = new DirectorySearcher(entry); searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + userName + "))"; SearchResult result = searcher.FindOne(); if (result != null) { //获取要修改密码的用户账号路径 DirectoryEntry userEntry = result.GetDirectoryEntry(); //修改密码 userEntry.Invoke("SetPassword", newPassword); userEntry.CommitChanges(); Console.WriteLine("密码修改成功!"); } else { Console.WriteLine("未找到要修改密码的用户账号!"); } ``` 以上代码中,需要根据实际情况修改domainName、adminName、adminPassword、userName和newPassword等参数,其中adminName和adminPassword是AD域管理员账号和密码,用于连接AD域。 在代码中,首先创建了一个AD连接,并指定要修改密码的用户账号路径,然后根据用户名查询该用户的账号信息,如果找到用户账号,则获取该用户的DirectoryEntry对象,并调用其SetPassword方法修改密码,最后提交更改即可。 注意:修改AD密码需要有足够的权限,否则会抛出异常或者修改失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值