【问题标题】:Ldap find one query returns null result when deployed to IIS 10部署到 IIS 10 时,Ldap 查找一个查询返回空结果
【发布时间】:2019-12-12 14:49:33
【问题描述】:

我编写了一个 LDAP 查询来使用用户名查找用户组。当我在 iis express 上的 VS2012 中运行它时,它工作正常。部署到 IIS 10 时,它总是返回 null 结果。

string _path = Convert.ToString(WebConfigurationManager.AppSettings["LDAPROOTURL"]);
string strName = HttpContext.Current.Request.LogonUserIdentity.Name.ToString().Split('\\')[1].Replace(".", " ");
string ldapname = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(strName);

DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + ldapname + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();

try
{
    SearchResult result = search.FindOne();
    if (result != null)
    {
        int propertyCount = result.Properties["memberOf"].Count;

        String dn;
        int equalsIndex, commaIndex;

        for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
        {
            dn = (String)result.Properties["memberOf"][propertyCounter];

            equalsIndex = dn.IndexOf("=", 1);
            commaIndex = dn.IndexOf(",", 1);
            if (-1 == equalsIndex)
            {
                return null;
            }

            groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
            groupNames.Append("|");

        }
    }
}
catch (Exception ex)
{
    throw new Exception("Error obtaining group names. " + ex.Message);
}
return Convert.ToString(groupNames);

仅在 Windows 身份验证中启用。 任何想法我做错了什么,为什么在 IIS 10 中部署后查询不起作用?

【问题讨论】:

    标签: asp.net ldap windows-authentication ldap-query iis-10


    【解决方案1】:

    尝试将HttpContext.Current.Request.LogonUserIdentity.Name 替换为HttpContext.Current.User.Identity.Name,看看是否会得到不同的结果。

    如果 HttpContext.Current.User.Identity.Name 为空,则您的 Windows 身份验证无法正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 2021-11-20
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多