卖逼视频免费看片|狼人就干网中文字慕|成人av影院导航|人妻少妇精品无码专区二区妖婧|亚洲丝袜视频玖玖|一区二区免费中文|日本高清无码一区|国产91无码小说|国产黄片子视频91sese日韩|免费高清无码成人网站入口

操作AD域net

.net 操作AD 域(轉(zhuǎn))上2011-06-14 16:21using System;using System.Collections.Generic;using System.Linq;using

.net 操作AD 域(轉(zhuǎn))上

2011-06-14 16:21

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Configuration;

using System.DirectoryServices;

namespace OperateADLibrary

{

public class OperateAD

{

///

/// 域名

///

private string _domain;

///

/// 主機域IP

///

private string _domainIp;

///

/// 管理員賬號

///

private string adminUser;

///

/// 管理員密碼

///

private string adminPwd;

///

/// 路徑的最前端

///

private string _ldapIdentity;

///

/// 路徑的最后端

///

private string _suffixPath;

#region 構(gòu)造函數(shù)

///

/// 構(gòu)造函數(shù)

/// 從webConfig 的AppSettings 屬性讀取值初始化字段 ///

public OperateAD(string domain, string domainIp, string adUser, string adPwd)

,

{

//_domain =

System.Configuration.ConfigurationManager.AppSettings["Domain"].ToString();

//_domainIp =

System.Configuration.ConfigurationManager.AppSettings["DomainIp"].ToString();

//adminUser =

System.Configuration.ConfigurationManager.AppSettings["ADAdminUser"].ToString();

//adminPwd =

System.Configuration.ConfigurationManager.AppSettings["ADAdminPassword"].ToString(); //_ldapIdentity = "LDAP://" _domainIp "/";

//_suffixPath = "DC=" _domain ",DC=COM";

//_domain = "bdxy";

//_domainIp = "10.1.209.197";

//adminUser = "administrator";

//adminPwd = "123456";

_domain = domain;

_domainIp = domainIp;

adminUser = adUser;

adminPwd = adPwd;

_ldapIdentity = "LDAP://" _domainIp "/";

_suffixPath = "DC=" _domain ",DC=com";

}

#endregion

#region 組織結(jié)構(gòu)下添加AD 賬戶

///

/// 添加AD 賬戶

///

/// 組織名稱

/// 域賬戶

/// 添加是否成功

public bool AddADAccount(string organizeName, DomainUser user)

{

DirectoryEntry entry = null;

try

{

if (ExitOU(organizeName) && user != null)

{

entry = new DirectoryEntry(GetOrganizeNamePath(organizeName), adminUser, adminPwd, AuthenticationTypes.Secure);

//增加賬戶到域中

DirectoryEntry NewUser = entry.Children.Add("CN=" user.UserName, "user"); NewUser.Properties["sAMAccountName"].Add(user.UserName); //account

,

NewUser.Properties["userPrincipalName"].Value = user.UserPrincipalName; //user logon name,xxx@bdxy.com

NewUser.Properties["givenName"].Value = "New User";//名

NewUser.Properties["initials"].Value = "Ms";

NewUser.Properties["name"].Value = "12";//full name

NewUser.Properties["sn"].Value = user.UserId;

NewUser.Properties["displayName"].Value = user.UserName;

NewUser.Properties["company"].Value = "1234";

NewUser.Properties["physicalDeliveryOfficeName"].Value =

user.PhysicalDeliveryOfficeName;

NewUser.Properties["Department"].Value = user.Department;

if (user.Telephone != null && user.Telephone != "")

{

NewUser.Properties["telephoneNumber"].Value = user.Telephone; }

if (user.Email != null && user.Email != "")

{

NewUser.Properties["mail"].Value = user.Email;

}

if (user.Description != null && user.Description != "")

{

NewUser.Properties["description"].Value = user.Description; }

NewUser.CommitChanges();

//設(shè)置密碼

//反射調(diào)用修改密碼的方法(注意端口號的問題 端口號會引起方法調(diào)用異常) NewUser.Invoke("SetPassword", new object[] { user.UserPwd }); //默認(rèn)設(shè)置新增賬戶啟用

NewUser.Properties["userAccountControl"].Value = 0x200;

NewUser.CommitChanges();

//DomainUser._success = "賬戶添加成功!";

return true;

}

else

{

//DomainUser._failed = "在域中不存在直屬組織單位";

return false;

}

}

,

catch (System.DirectoryServices.DirectoryServicesCOMException ex) {

//DomainUser._failed = "賬戶添加失敗!" ex.Message.ToString(); return false;

}

finally

{

if (entry != null)

{

entry.Dispose();

}

}

}

#endregion

#region 重命名賬戶

///

/// 重命名賬戶

///

/// 管理員名稱

/// 管理員密碼

/// 原用戶名

/// 新用戶名

public bool RenameUser(string oldUserName, string newUserName) {

try

{

DirectoryEntry userEntry = FindObject("user", oldUserName); if (userEntry != null)

{

userEntry.Rename("CN=" newUserName);

userEntry.CommitChanges();

//DomainUser._success = "重命名成功!";

return true;

}

//DomainUser._failed = "沒找到用戶!" oldUserName; return false;

}

catch (Exception ex)

{

//DomainUser._failed = "重命名失?。? ex.Message.ToString(); return false;

}

}

,

#endregion

#region 設(shè)置用戶密碼

///

/// 設(shè)置用戶密碼

///

/// 用戶名

/// 密碼

public bool SetUserPassword(string userName, string password)

{

try

{

DirectoryEntry userEntry = FindObject("user", userName);

if (userEntry != null)

{

userEntry.Invoke("SetPassword", new object[] { password }); userEntry.CommitChanges();

//DomainUser._success = "密碼設(shè)置成功!";

return true;

}

//DomainUser._failed = "沒找到用戶!" userName;

return false;

}

catch (Exception ex)

{

//DomainUser._failed = "密碼設(shè)置失?。? ex.Message.ToString(); return false;

}

}

#endregion

#region 修改密碼

///

/// 修改密碼

///

/// 用戶

/// 舊密碼

/// 新密碼

public bool ChangePassword(string username, string oldpwd, string newpwd) {

try

{

DirectoryEntry entry = FindObject("user", username);

if (entry != null)

,

{

// to-do: 需要解決密碼策略問題

entry.Invoke("ChangePassword", new object[] {oldpwd, newpwd }); entry.CommitChanges();

entry.Close();

// DomainUser._success = "密碼修改成功!";

return true;

}

else

{

// DomainUser._failed = "沒找到用戶!" username;

return false;

}

}

catch (Exception ex)

{

//DomainUser._failed = "密碼修改失??!" ex.Message.ToString();

return false;

}

}

#endregion

#region 刪除賬戶

///

/// 刪除AD 賬戶,使用當(dāng)前上下文的安全信息

///

/// 用戶名稱

public bool DeleteADAccount(string userName)

{

try

{

DirectoryEntry user = FindObject("user", userName);

if (user != null)

{

using (DirectoryEntry de = new DirectoryEntry(user.Parent.Path, adminUser, adminPwd))

{

de.Children.Remove(user);

de.CommitChanges();

//DomainUser._success = "賬戶刪除成功!";

return true;

}

}

,

// DomainUser._failed = "未找到賬戶!";

return false;

}

catch (Exception ex)

{

//DomainUser._failed = "賬戶刪除失敗!" ex.Message.ToString(); return false;

}

}

#endregion

.net 操作AD 域(轉(zhuǎn)) 中

2011-06-14 16:22

#region 創(chuàng)建OU

///

/// 創(chuàng)建OU

///

/// 管理員名稱

/// 管理員密碼

/// 創(chuàng)建的OU 名稱

/// 父組織單位

/// 目錄實體

public DirectoryEntry CreateOrganizeUnit(string name, string parentOrganizeUnit) {

DirectoryEntry parentEntry = null;

try

{

//示例頂級" LDAP://10.1.209.197/dc=bdxy,dc=com"

parentEntry = new DirectoryEntry(GetOrganizeNamePath(parentOrganizeUnit), adminUser, adminPwd,

AuthenticationTypes.Secure);

DirectoryEntry organizeEntry = parentEntry.Children.Add("OU=" name, "organizationalUnit");

organizeEntry.CommitChanges();

//DomainUser._success = "組織單位添加成功!";

return organizeEntry;

}

catch (System.DirectoryServices.DirectoryServicesCOMException ex)

{

//DomainUser._failed = "添加組織單位失敗!" ex.Message.ToString(); return new DirectoryEntry();

}

finally

{

if (parentEntry != null)

,

{

parentEntry.Dispose();

}

}

}

#endregion

#region 刪除OU

///

/// 刪除OU

///

/// 創(chuàng)建的OU 名稱

/// 父組織單位

/// 目錄實體

public bool DeleteOrganizeUnit(string name, string parentOrganizeUnit)

{

DirectoryEntry parentEntry = null;

try

{

//示例頂級" LDAP://10.1.209.197/dc=bdxy,dc=com"

parentEntry = new DirectoryEntry(GetOrganizeNamePath(parentOrganizeUnit), adminUser, adminPwd,

AuthenticationTypes.Secure);

DirectoryEntry organizeEntry = parentEntry.Children.Find("OU=" name, "organizationalUnit");

//先刪除組織單元下的用戶或者組

parentEntry.Children.Remove(organizeEntry);

organizeEntry.CommitChanges();

//DomainUser._success = "組織單位刪除成功!";

return true;

}

catch (System.DirectoryServices.DirectoryServicesCOMException ex)

{

//DomainUser._failed = "組織單位刪除失??!" ex.Message.ToString(); return false;

}

finally

{

if (parentEntry != null)

{

parentEntry.Dispose();

}

}

}

,

#endregion

#region 創(chuàng)建組

///

/// 創(chuàng)建組

///

/// 組名

/// 組織單位

/// 是否創(chuàng)建成功

public bool CreateGroup(string name, string OrganizeUnit)

{

DirectoryEntry parentEntry = null;

try

{

parentEntry = new DirectoryEntry(GetOrganizeNamePath(OrganizeUnit), adminUser, adminPwd,

AuthenticationTypes.Secure);

DirectoryEntry groupEntry = parentEntry.Children.Add("CN=" name, "group"); groupEntry.CommitChanges();

// DomainUser._success = "組創(chuàng)建成功!";

return true;

}

catch (System.DirectoryServices.DirectoryServicesCOMException ex)

{

//DomainUser._failed = "組創(chuàng)建失敗!" ex.Message.ToString();

return false;

}

finally

{

if (parentEntry != null)

{

parentEntry.Dispose();

}

}

}

#endregion

#region 刪除組

///

/// 刪除組

///

/// 組名

/// 組織單位

,

/// 是否創(chuàng)建成功

public bool DeleteGroup(string name, string OrganizeUnit)

{

DirectoryEntry parentEntry = null;

try

{

parentEntry = new DirectoryEntry(GetOrganizeNamePath(OrganizeUnit), adminUser, adminPwd,

AuthenticationTypes.Secure);

DirectoryEntry groupEntry = parentEntry.Children.Find("CN=" name, "group"); parentEntry.Children.Remove(groupEntry);

groupEntry.CommitChanges();

//DomainUser._success = "組刪除成功!";

return true;

}

catch (System.DirectoryServices.DirectoryServicesCOMException ex)

{

// DomainUser._failed = "組刪除失敗!" ex.Message.ToString();

return false;

}

finally

{

if (parentEntry != null)

{

parentEntry.Dispose();

}

}

}

#endregion

#region 將用戶加入到用戶組中

///

/// 將用戶加入到用戶組中

///

/// 用戶名

/// 組織名

/// 組名

/// 組所在路徑

/// 用戶名或用戶組不存在 public bool AddUserToGroup(string userName, string groupName, string groupPath) {

DirectoryEntry group = null;

標(biāo)簽: