我花了一天多地发现,主要目的是使用方式更多的带宽比使用的DirectoryServices。情景就是这样。我有一个组里面有3000个计算机对象。要检查计算机是否该组中我检索到的GroupPrincipal和搜索的ComputerPrincipal。网络利用率 - AccountManagement对比的DirectoryServices
Boolean _retVal = false;
PrincipalContext _principalContext = null;
using (_principalContext = new PrincipalContext(ContextType.Domain, domainController, srv_user, srv_password)) {
ComputerPrincipal _computer = ComputerPrincipal.FindByIdentity(_principalContext, accountName);
GroupPrincipal _grp = GroupPrincipal.FindByIdentity(_principalContext, groupName);
if (_computer != null && _grp != null) {
// get the members
PrincipalSearchResult<Principal> _allGrps = _grp.GetMembers(false);
if (_allGrps.Contains(_computer)) {
_retVal = true;
}
else {
_retVal = false;
}
}
}
return _retVal;
其实非常漂亮的界面,但是这会为每个请求创建大约12MB的流量。如果您的域控制器位于局域网中,则不存在问题。如果您使用WAN访问域控制器,则会终止连接/应用程序。
我注意到了这一点后,我使用的DirectoryServices
Boolean _retVal = false;
DirectoryContext _ctx = null;
try {
_ctx = new DirectoryContext(DirectoryContextType.DirectoryServer, domainController, srv_user, srv_password);
} catch (Exception ex) {
// do something useful
}
if (_ctx != null) {
try {
using (DomainController _dc = DomainController.GetDomainController(_ctx)) {
using (DirectorySearcher _search = _dc.GetDirectorySearcher()) {
String _groupToSearchFor = String.Format("CN={0},", groupName);
_search.PropertiesToLoad.Clear();
_search.PropertiesToLoad.Add("memberOf");
_search.Filter = String.Format("(&(objectCategory=computer)(name={0}))", accountName); ;
SearchResult _one = null;
_one = _search.FindOne();
if (_one != null) {
int _count = _one.Properties["memberOf"].Count;
for (int i = 0; i < _count; i++) {
string _m = (_one.Properties["memberOf"][i] as string);
if (_m.Contains(groupName)) { _retVal = true; }
}
}
}
}
} catch (Exception ex) {
// do something useful
}
}
return _retVal;
此实现将使用网络流量12K左右重新实现相同的功能。这可能不是很好,但可以节省很多带宽。
我的问题是现在如果有人有一个想法是什么AccountManagement对象是做它使用这么多带宽?
谢谢!
我正在开发与AccountManagement接口的东西,我很乐意更多地了解这个... – Emmanuel 2011-03-21 16:06:59
我很高兴为您节省一些时间:-) – auhorn 2011-03-22 12:25:58