一切似乎都运行正常(除了检索特定属性应该比迭代所有现有属性更容易 - 不知道为什么它是这样设计的),但是当我尝试断开时我得到
PlatformNotSupportedException:此平台不支持线程中止。
代码几乎完全取自Novell文档,除了我试图只接收2个特定属性(名称和登录时间)的事实:
var users = new List<User>();
// Creating an LdapConnection instance
LdapConnection ldapConn= new LdapConnection();
//Connect function will create a socket connection to the server
ldapConn.Connect("DOMAIN.com",389);
//Bind function will Bind the user object Credentials to the Server
ldapConn.Bind("DOMAIN\\_Bassi","password");
// Searches in the Marketing container and return all child entries just below this
//container i.e. Single level search
LdapSearchResults lsc=ldapConn.Search("OU=Users,OU=MCS,OU=AU,OU=COMPANY,DC=DOMAIN,DC=com",
LdapConnection.SCOPE_ONE,
"objectClass=*",
null,
false);
while (lsc.hasMore())
{
var user = new User();
LdapEntry nextEntry = null;
try
{
nextEntry = lsc.next();
}
catch(LdapException e)
{
Console.WriteLine("Error: " + e.LdapErrorMessage);
// Exception is thrown, go for next entry
continue;
}
Console.WriteLine("\n" + nextEntry.DN);
LdapAttributeSet attributeSet = nextEntry.getAttributeSet();
System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
var atts = attributeSet.ToArray().ToList();
while(ienum.MoveNext())
{
LdapAttribute attribute=(LdapAttribute)ienum.Current;
if(attribute.Name == "sAMAccountName")
{
user.UserName = attribute.StringValue;
}
else if(attribute.Name == "lastLogonTimestamp")
{
user.LastLogon = DateTime.FromFileTime(long.Parse(attribute.StringValue));
}
}
users.Add(user);
}
ldapConn.Disconnect();
一旦Disconnect
被调用我就得到了错误。
有没有其他人经历过这个或知道可能出现什么问题?
堆栈跟踪
System.Threading.Thread.Abort()
Novell.Directory.Ldap.Connection.Dispose(bool disposing, string reason, int semaphoreId, InterThreadException notifyUser)
Novell.Directory.Ldap.Connection.destroyClone(bool apiCall)
Novell.Directory.Ldap.LdapConnection.Disconnect()
LogonChecker.Managers.ActiveDirectory.GetADUsers() in ActiveDirectory.cs
-
64. }
65. }
66.
67. users.Add(user);
68. }
69.
70. ldapConn.Disconnect();
71.
72. return users;
73.
74. // LdapConnection ADconn = new LdapConnection();
75. // ADconn.Connect("DOMAIN.com", 389);
76. // ADconn.Bind("DOMAIN\\_Bassie", "password");
LogonChecker.Controllers.UserController+<Index>d__2.MoveNext() in UserController.cs
-
20. }
21.
22. // GET: User
23. public async Task<IActionResult> Index()
24. {
25. var ad = new ActiveDirectory();
26. var users = ad.GetADUsers();
27.
28. return View(users);
29. // return View(await _context.User.ToListAsync());
30. }
31.
32. // GET: User/Details/5
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeNextActionFilterAsync>d__10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeInnerFilterAsync>d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()
Novell开发人员提出了Novell.Directory.Ldap.NETStandard的new version,它似乎与DotNet Core 2.0完全兼容。我已将项目引用更改为Novell.Directory.Ldap.NETStandard 3.0.0版本,现在它正常工作。
命令行为:
Install-Package Novell.Directory.Ldap.NETStandard -Version 3.0.0-beta4
抛出此错误的原因是因为.NET Core中不支持Thread.Abort()
。需要更新库以使用不同的机制(我相信CancellationToken是推荐的方法)