我正在拼凑一个快速的C#win表单应用程序,以帮助解决重复的文书工作.
我已在AD中为所有用户帐户执行搜索,并将其添加到带有复选框的列表视图中.
我想默认listviewitems的默认检查状态取决于帐户的启用/禁用状态.
string path = "LDAP://dc=example,dc=local";
DirectoryEntry directoryRoot = new DirectoryEntry(path);
DirectorySearcher searcher = new DirectorySearcher(directoryRoot,
"(&(objectClass=User)(objectCategory=Person))");
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
DirectoryEntry de = result.GetDirectoryEntry();
ListViewItem lvi = new ListViewItem(
(string)de.Properties["SAMAccountName"][0]);
// lvi.Checked = (bool) de.Properties["AccountEnabled"]
lvwUsers.Items.Add(lvi);
}
Run Code Online (Sandbox Code Playgroud)
我正在努力找到正确的属性进行解析以从DirectoryEntry对象获取帐户的状态.我搜索了AD用户属性,但没有找到任何有用的东西.
谁能提供任何指针?
我在其他三台服务器上使用相同的Web应用程序.任何人都知道为什么不在第四台服务器上工作?查看错误和堆栈跟踪:
发生了操作错误.
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:
System.DirectoryServices.DirectoryServicesCOMException:发生操作错误.来源错误:
在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.
堆栈跟踪:
[DirectoryServicesCOMException(0x80072020):发生操作错误.] System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)+454 System.DirectoryServices.DirectoryEntry.Bind()+36 System.DirectoryServices.DirectoryEntry.get_AdsObject()+31 System.DirectoryServices.PropertyValueCollection.PopulateList()+22
System.DirectoryServices .PropertyValueCollection..ctor(DirectoryEntry entry,String propertyName)+96
System.DirectoryServices.PropertyCollection.get_Item(String propertyName)+142 System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()+1134 System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit( )+37 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()+124 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()+31 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context,Type principalType,Nullable'1 identityType, String identityValue,DateTime refDate)+14
System.DirectoryServices.AccountMan agement.Principal.FindByIdentityWithType(PrincipalContext context,Type principalType,String identityValue)+73
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context,String identityValue)+25
Infraero.TINE3.STTEnterprise.Web.Common.Seguranca.ServicoAutenticacao.EfetuarLogin (AcessoUsuario acessoUsuario,String senha)在D:\ SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Common\Seguranca\ServicoAutenticacao.cs:34 Infraero.TINE3.STTEnterprise.Web. Controllers.LoginController.ValidarUsuarioAD(String matricula,String senha,AcessoUsuario acessoUsuario)在D:\ SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs:92 Infraero. TINE3.STTEnterprise.Web.Controllers.LoginController.ValidarUsuario(String matricula,String senha)在D:\ SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs: 80 Infraero.TINE3.D:\ SVN\STT\trunk\4-0_CodigoFonte_Enterprise\4-4_SRC\Infraero.TINE3.STTEnterprise.Web\Controllers\LoginController.cs中的STTEnterprise.Web.Controllers.LoginController.Index(LoginViewModel loginViewModel):54 lambda_method(Closure, ControllerBase,Object [])+ 108
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object [] parameters)+17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary'2 parameters)+208
System. Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary'2 parameters)+27
System.Web.Mvc.<> c__DisplayClass15.b__12()+55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter,ActionExecutingContext …
我正在尝试使用.Net中的目录服务运行简单的LDAP查询.
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://someserver.contoso.com/DC=contoso,DC=com");
directoryEntry.AuthenticationType = AuthenticationTypes.Secure;
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
directorySearcher.Filter = string.Format("(&(objectClass=user)(objectCategory=user) (sAMAccountName={0}))", username);
var result = directorySearcher.FindOne();
var resultDirectoryEntry = result.GetDirectoryEntry();
return resultDirectoryEntry.Properties["msRTCSIP-PrimaryUserAddress"].Value.ToString();
Run Code Online (Sandbox Code Playgroud)
我得到以下例外:
System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
Run Code Online (Sandbox Code Playgroud)
作为控制台应用中的代码段,这是有效的.但是,当我将其作为WCF服务的一部分运行(在相同的凭据下运行)时,它会抛出上述异常.
有什么建议?
谢谢
我已经查过这篇文章了.但它没有回答我的问题.我想获取特定用户所属的所有活动目录组.
我写了以下代码.但我无法继续进行,因为我不知道如何提供过滤器以及如何访问属性.
class Program
{
static void Main(string[] args)
{
DirectoryEntry de = new DirectoryEntry("LDAP://mydomain.com");
DirectorySearcher searcher = new DirectorySearcher(de);
searcher.Filter = "(&(ObjectClass=group))";
searcher.PropertiesToLoad.Add("distinguishedName");
searcher.PropertiesToLoad.Add("sAMAccountName");
searcher.PropertiesToLoad.Add("name");
searcher.PropertiesToLoad.Add("objectSid");
SearchResultCollection results = searcher.FindAll();
int i = 1;
foreach (SearchResult res in results)
{
Console.WriteLine("Result" + Convert.ToString(i++));
DisplayProperties("distinguishedName", res);
DisplayProperties("sAMAccouontName", res);
DisplayProperties("name", res);
DisplayProperties("objectSid", res);
Console.WriteLine();
}
Console.ReadKey();
}
private static void DisplayProperties(string property, SearchResult res)
{
Console.WriteLine("\t" + property);
ResultPropertyValueCollection col = res.Properties[property];
foreach (object o in col)
{
Console.WriteLine("\t\t" …
Run Code Online (Sandbox Code Playgroud) 我有一组将要创建的测试帐户,但帐户将设置为在首次登录时要求更改密码.我想用C#编写一个程序来浏览测试帐户并更改密码.
我看到使用的Active Directory示例PrincipalSearcher
和其他使用相同内容的示例DirectorySearcher
.这两个例子有什么区别?
使用示例 PrincipalSearcher
PrincipalContext context = new PrincipalContext(ContextType.Domain);
PrincipalSearcher search = new PrincipalSearcher(new UserPrincipal(context));
foreach( UserPrincipal user in search.FindAll() )
{
if( null != user )
Console.WriteLine(user.DistinguishedName);
}
Run Code Online (Sandbox Code Playgroud)
使用示例 DirectorySearcher
DirectorySearcher search = new DirectorySearcher("(&(objectClass=user)(objectCategory=person))");
search.PageSize = 1000;
foreach( SearchResult result in search.FindAll() )
{
DirectoryEntry user = result.GetDirectoryEntry();
if( null != user )
Console.WriteLine(user.Properties["distinguishedName"].Value.ToString());
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试System.DirectoryServices
在网站项目中使用,我收到此错误:
命名空间"系统"中不存在类型或命名空间名称"DirectoryServices"(您是否缺少程序集引用?)
我的项目参考System.DirectoryServices
了web.config
:
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
Run Code Online (Sandbox Code Playgroud)
我确实有using System.DirectoryServices
我想要使用它的文件.
有没有人知道在哪里寻找问题?
我有一个漫长的运行过程,需要经常在Active Directory上进行大量查询.为此,我一直在使用Directory.DirectoryServices命名空间,使用DirectorySearcher和DirectoryEntry类.我注意到应用程序中存在内存泄漏.
可以使用以下代码复制:
while (true)
{
using (var de = new DirectoryEntry("LDAP://hostname", "user", "pass"))
{
using (var mySearcher = new DirectorySearcher(de))
{
mySearcher.Filter = "(objectClass=domain)";
using (SearchResultCollection src = mySearcher.FindAll())
{
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这些类的文档说如果没有调用Dispose(),它们将泄漏内存.我试过没有处理过,它只是泄漏了更多的内存.我用框架版本2.0和4.0测试了这个以前有人遇到过这个吗?有没有解决方法?
更新:我尝试在另一个AppDomain中运行代码,它似乎也没有帮助.
我使用以下代码创建一个应用程序池:
var metabasePath = string.Format(@"IIS://{0}/W3SVC/AppPools", serverName);
DirectoryEntry newpool;
DirectoryEntry apppools = new DirectoryEntry(metabasePath);
newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
newpool.CommitChanges();
Run Code Online (Sandbox Code Playgroud)
如何指定应用程序池应使用.NET Framework 4.0?
我收到以下与使用DirectorySearcher.FindOne()
或查询AD相关的间歇性错误
FindAll()
.
System.OverflowException: Arithmetic operation resulted in an overflow.
at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.GetCurrentResult()
at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.get_Current()
at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.System.Collections.IEnumerator.get_Current()
Run Code Online (Sandbox Code Playgroud)
这是在一个Web应用程序中发生的,似乎是在应用程序运行了几个小时后发生的.
这是Microsoft Connect上的文档问题,但看起来它已标记为"不可重现"并已关闭.
我在这里找到的唯一解决方案是定期回收应用程序池,这是一个相当苛刻的解决方法,当用户处于工作中时不可行.
有没有人经历过这个,如果是这样,这是如何解决的?
我曾尝试使用缓存,但这只会延迟不可避免的事情,直到你达到AD调用次数的某个阈值,因为人们已经报告每次调用API时它会泄漏内存.
任何帮助将非常感激.