asp.net – System.DirectoryServices – 服务器不可操作

前端之家收集整理的这篇文章主要介绍了asp.net – System.DirectoryServices – 服务器不可操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到一个网站的错误,我使用 Windows身份验证.

奇怪的东西:

>仅当用户尚未保存到数据库(新的未知用户)时才会发生
>只出现在现场系统上,一切都在本地开发环境中

这是我在日志邮件中得到的:

Source : System.DirectoryServices

Message: The server is not operational.

Trace:
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()
at Smarthouse.Labs.DataAccess.UserListManager.SaveUser(String windowsUserName)

这是我如何实现DirectorySearch:

  1. private void SaveUser(string windowsUserName)
  2. {
  3. string[] domainAndUser = windowsUserName.Split('\\');
  4. string domain = domainAndUser[0];
  5. string username = domainAndUser[1];
  6.  
  7. DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain);
  8. DirectorySearcher search = new DirectorySearcher(entry);
  9.  
  10. try
  11. {
  12. // Bind to the native AdsObject to force authentication.
  13. search.Filter = "(SAMAccountName=" + username + ")";
  14. search.PropertiesToLoad.Add("cn");
  15. search.PropertiesToLoad.Add("sn");
  16. search.PropertiesToLoad.Add("givenName");
  17. search.PropertiesToLoad.Add("mail");
  18.  
  19. SearchResult result = search.FindOne();
  20.  
  21. if (result == null)
  22. {
  23. throw new Exception("No results found in Windows authentication.");
  24. }
  25.  
  26. User userToSave = new User();
  27. userToSave.FirstName = (String) result.Properties["givenName"][0];
  28. userToSave.LastName = (String) result.Properties["sn"][0];
  29. userToSave.Email = (String) result.Properties["mail"][0];
  30. userToSave.Username = windowsUserName;
  31. userToSave.Guid = Guid.NewGuid();
  32.  
  33. SaveUser(userToSave);
  34. }
  35. catch (Exception ex)
  36. {
  37. throw new Exception("Error authenticating user. " + ex.Message,ex);
  38. }
  39. finally
  40. {
  41. //Dispose service and search to prevent leek in memory
  42. entry.Dispose();
  43. search.Dispose();
  44. }
  45. }

如果需要更多的代码示例,请告诉我.

解决方法

您的问题是您正在使用“简单”域名进行绑定 – 这在LDAP中不起作用.实际上,如果你尝试绑定到LDAP:// MyDomain,你真正在做的是试图绑定到名为MyDomain的服务器.

您需要一个有效的LDAP绑定字符串,如LDAP:// dc = yourdomain,dc = local或something.

要了解您的默认LDAP绑定上下文是什么,请使用此代码段:

  1. DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");
  2.  
  3. if (deRoot != null)
  4. {
  5. string defaultNamingContext = deRoot.Properties["defaultNamingContext"].Value.ToString();
  6. }

一旦你有这个字符串 – 将它用作你的LDAP服务器的绑定字符串.

如果您使用的是.NET 3.5及更高版本,则应查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间.在这里阅读全文:

> Managing Directory Security Principals in the .NET Framework 3.5
> MSDN docs on System.DirectoryServices.AccountManagement

基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:

  1. // set up domain context -- no domain name needed,uses default domain
  2. PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
  3.  
  4. // find a user
  5. UserPrincipal user = UserPrincipal.FindByIdentity(ctx,username);
  6.  
  7. if(user != null)
  8. {
  9. // do something here....
  10. }

新的S.DS.AM让您很容易在广告中与用户和群体一起玩耍!

猜你在找的asp.Net相关文章