using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.DirectoryServices;
using System.Web.Configuration;
using System.Collections;
/// <summary>
/// ADOprate 的摘要说明
/// </summary>
public class ADOprate
{
private string _ADPath; //域路径
private string _Domain; //域名称
private string _userID;//要模拟的用户名称
private string _Pass; //用户密码
public IdentityImpersonation _identityimpersonation;//包含程序模拟方法的类实例
//默认构造函数,初始化,并读取配置文件指定信息
public ADOprate(string userID, string Pass)
{
this._ADPath = System.Configuration.ConfigurationManager.AppSettings["LDAPString"];
this._Domain = "GreatWall";
this._userID = userID;
this._Pass = Pass;
this._identityimpersonation = new IdentityImpersonation(this._userID,this._Pass,this._Domain);
//获取配置文件信息
//Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//SystemWebSectionGroup ws = (SystemWebSectionGroup)config.GetSectionGroup("system.web");
//IdentitySection iden = ws.Identity;
//this._userID = iden.UserName;
//this._Pass = iden.Password;
}
//创建要模拟运行的用户,以获取权限
private DirectoryEntry GetDirectoryObject()
{
DirectoryEntry entry = new DirectoryEntry(_ADPath, this._userID, this._Pass, AuthenticationTypes.Secure);
return entry;
}
//创建属于用户自己的身份,并具有默认权限
private DirectoryEntry GetDirectoryObject(string userID, string UserPass)
{
DirectoryEntry entry = new DirectoryEntry(_ADPath, userID, UserPass, AuthenticationTypes.Secure);
return entry;
}
//根据指定用户名和密码判断是否合法用户,并返回用户信息;
public UserEntity IsValidateUser(string userID, string pwd)
{
if (this._identityimpersonation.LookUpAccount(userID))
{
if (this._identityimpersonation.BeginImpersonate())
{
string domainAnduserID = this._Domain + @"\" + userID;
DirectoryEntry entry = new DirectoryEntry(_ADPath, domainAnduserID, pwd);
try
{
Object obj = entry.NativeObject;
}
catch
{
return null;
}
this._userID = userID;
this._Pass = pwd;
UserEntity user = GetUserMsg(userID);
this._identityimpersonation.StopImpersonate();
return user;
}
}
return null;
}
private bool IsManager(DirectoryEntry entry, string userID)
{
PropertyValueCollection groups = entry.Properties["memberOf"];
foreach (object PropertyValue in groups)
{
if (PropertyValue.ToString().IndexOf("OU") != -1)
return true;
}
return false;
}
//返回指定用户的节点对象
public UserEntity GetUserMsg(string userID)
{
if (this._identityimpersonation.LookUpAccount(userID))
{
if (this._identityimpersonation.BeginImpersonate())
{
DirectorySearcher search = new DirectorySearcher(GetDirectoryObject());
search.Filter = "(SAMAccountName=" + userID + ")";
//search.PropertiesToLoad.Add("CN");
SearchResult result = search.FindOne();
if (null == result)
{
this._identityimpersonation.StopImpersonate();
return null;
}
else
{
DirectoryEntry entry = new DirectoryEntry(result.Path);
UserEntity userEntity = new UserEntity();
userEntity.UserID = userID;
userEntity.DePartName = entry.Properties["description"][0].ToString();
userEntity.UserName = entry.Properties["CN"][0].ToString();
//设置其权限
if (IsManager(entry, userID))
userEntity.PositionLevel = "1";
else userEntity.PositionLevel = "-1";
this._identityimpersonation.StopImpersonate();
return userEntity;
}
}
}
return null;
}
//更具组名返回属于该组的所有成员信息
private SearchResultCollection GetUsersByGroup(string GroupName)
{
if (this._identityimpersonation.BeginImpersonate())
{
DirectorySearcher search = new DirectorySearcher(GetDirectoryObject());
search.Filter = "(description=" + GroupName + ")";
search.SearchScope = SearchScope.Subtree;
SearchResultCollection results = search.FindAll();
this._identityimpersonation.StopImpersonate();
return results;
}
return null;
}
//以二维数组的形式返回指定组下的所有成员信息
public string[,] GetUserMsgByGroup(string GroupName)
{
string[,] UsermsgArray = null;
System.Web.Caching.Cache cache = HttpRuntime.Cache;
if (cache[GroupName] != null)
{
// Create DataTable From Cache
UsermsgArray = (string[,])cache[GroupName];
}
else
if (this._identityimpersonation.BeginImpersonate())
{
SearchResultCollection results = GetUsersByGroup(GroupName);
int count = results.Count;
UsermsgArray = new string[count, 2];
int i = 0;
foreach (System.DirectoryServices.SearchResult result in results)
{
DirectoryEntry de = new DirectoryEntry(result.Path);
UsermsgArray[i, 0] = de.Properties["sAMAccountName"][0].ToString();
UsermsgArray[i, 1] = de.Properties["CN"][0].ToString();
i++;
}
this._identityimpersonation.StopImpersonate();
TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
cache.Insert(GroupName, UsermsgArray, null, DateTime.MaxValue, SessTimeOut,System.Web.Caching.CacheItemPriority.NotRemovable,null);
}
return UsermsgArray;
}
}