gpt4 book ai didi

asp.net - System.DirectoryServices - 服务器无法运行

转载 作者:行者123 更新时间:2023-12-03 20:23:47 28 4
gpt4 key购买 nike

我在使用 Windows 身份验证的网站上收到错误消息。

奇怪的事情:

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

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

    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 的方式:
    private void SaveUser(string windowsUserName)
    {
    string[] domainAndUser = windowsUserName.Split('\\');
    string domain = domainAndUser[0];
    string username = domainAndUser[1];

    DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain);
    DirectorySearcher search = new DirectorySearcher(entry);

    try
    {
    // Bind to the native AdsObject to force authentication.
    search.Filter = "(SAMAccountName=" + username + ")";
    search.PropertiesToLoad.Add("cn");
    search.PropertiesToLoad.Add("sn");
    search.PropertiesToLoad.Add("givenName");
    search.PropertiesToLoad.Add("mail");

    SearchResult result = search.FindOne();

    if (result == null)
    {
    throw new Exception("No results found in Windows authentication.");
    }

    User userToSave = new User();
    userToSave.FirstName = (String) result.Properties["givenName"][0];
    userToSave.LastName = (String) result.Properties["sn"][0];
    userToSave.Email = (String) result.Properties["mail"][0];
    userToSave.Username = windowsUserName;
    userToSave.Guid = Guid.NewGuid();

    SaveUser(userToSave);
    }
    catch (Exception ex)
    {
    throw new Exception("Error authenticating user. " + ex.Message, ex);
    }
    finally
    {
    //Dispose service and search to prevent leek in memory
    entry.Dispose();
    search.Dispose();
    }
    }

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

    最佳答案

    您的问题是您使用“普通”域名进行绑定(bind) - 这在 LDAP 中不起作用。实际上,如果您尝试绑定(bind)到 LDAP://MyDomain , 你是什么真的正在尝试绑定(bind)到 服务器 调用MyDomain .

    您需要一个有效的 LDAP 绑定(bind)字符串 - 类似于 LDAP://dc=yourdomain,dc=local或者其他的东西。

    要找出您的默认 LDAP 绑定(bind)上下文是什么,请使用以下代码片段:

    DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");

    if (deRoot != null)
    {
    string defaultNamingContext = deRoot.Properties["defaultNamingContext"].Value.ToString();
    }

    获得该字符串后 - 将其用作 LDAP 服务器的绑定(bind)字符串。

    如果您使用的是 .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 中轻松找到用户和/或组:
    // set up domain context -- no domain name needed, uses default domain 
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);

    if(user != null)
    {
    // do something here....
    }

    新的 S.DS.AM 使得在 AD 中与用户和组一起玩变得非常容易!

    关于asp.net - System.DirectoryServices - 服务器无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7931332/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com