【发布时间】:2012-11-21 19:17:26
【问题描述】:
我正在尝试编写一个实用方法来更新 C# 中的 AD 属性(现在只是单值字符串属性)。这是一个不依赖 IIS 的独立实用程序。此方法将用于将数据从我们的 HR 系统加载到我们的 AD 中。
我能够使用 System.DirectoryServices.Protocols 有效地读取对象和属性。但是当我调用 ModifyRequest 方法时,我得到一个 DirectoryOperationException 消息“服务器无法处理目录请求”。
基于另一个堆栈溢出问题: .Net's Directory Services throws a strange exception
我尝试将端口 636 用于 SSL LDAP,但它并没有改变行为。
我没有使用 IIS,我使用的是 .NET 4.5,因此不应该应用适用于 .NET/IIS 的 Microsoft 补丁。
谷歌搜索没有结果。
如果你知道为什么会出现这个错误,以及如何解决它,我将非常感激。
下面的代码.. 请假设 Conn 包含来自封闭实用程序类的有效且经过身份验证的 LDAP 连接——如果需要,我可以提供封闭实用程序类的完整源代码。
异常发生在ModifyStringAttributeValues 中的SendRequest 行:
using System;
using System.Collections.Generic;
using System.DirectoryServices.Protocols;
using System.Net;
namespace MyOrganization.Common.Ldap
{
public class LdapSession
{
public bool UseKerberos { set; get; }
public String Host { set; get; }
public String UserId { set; get; }
public String Password { set; get; }
public String Tag { set; get; }
public int Port { set; get; }
public const int DefaultLdapPort = 389;
protected LdapConnection Conn;
public void EstablishV2()
{
}
public void Establish()
{
var effectivePort = Port == 0 ? DefaultLdapPort : Port;
Console.WriteLine("EffectivePort={0}", effectivePort);
var identifier = new LdapDirectoryIdentifier(Host, effectivePort);
if (UseKerberos)
{
Conn = new LdapConnection(identifier)
{
AuthType = AuthType.Kerberos,
Timeout = new TimeSpan(0, 10, 0, 0),
SessionOptions =
{
ProtocolVersion = 3,
VerifyServerCertificate =
new VerifyServerCertificateCallback((con, cer) => true),
SecureSocketLayer = true
}
};
}
else
{
Conn = new LdapConnection(identifier)
{
AuthType = AuthType.Basic,
Timeout = new TimeSpan(0, 10, 0, 0)
};
// Console.WriteLine("LPA: Binding with {0}, {1}", UserId, Password); // QUARTZ
Conn.Bind(new NetworkCredential(UserId, Password));
}
}
public IEnumerable<SearchResultEntry> Search(string cx, string filter, SearchScope searchScope, params string[] attrib)
{
var s = new SearchRequest(cx, filter, searchScope, attrib)
{
SizeLimit = 0,
TimeLimit = new TimeSpan(1, 0, 0) // One hour, zero minutes, zero seconds
};
var raw = Conn.SendRequest(s);
if (raw == null)
{
throw new Exception("null response");
}
var r = raw as SearchResponse;
if (r != null)
{
// Console.WriteLine(Tag + "Search response entries: {0}", r.Entries.Count); // QUARTZ
foreach (SearchResultEntry e in r.Entries)
{
yield return e;
}
}
else
{
// Console.WriteLine(Tag + "Search response was null" ); // QUARTZ
}
yield break;
}
public ResultCode ModifyStringAttributeValues(string dn, IDictionary<string, string> modifications)
{
// declare the request and response objects here
// they are used in two blocks
ModifyRequest modRequest;
ModifyResponse modResponse;
try
{
// initialize the modRequest object
modRequest =
new ModifyRequest(dn);
modRequest.Controls.Add(new PermissiveModifyControl());
var mods = new DirectoryAttributeModification[modifications.Count];
int z = 0;
foreach (var pair in modifications)
{
var mod = new DirectoryAttributeModification();
mod.Operation = DirectoryAttributeOperation.Replace;
mod.Name = pair.Key;
mod.Add(pair.Value);
mods[z] = mod;
z += 1;
}
// cast the returned directory response into a ModifyResponse type
// named modResponse
modResponse =
(ModifyResponse)Conn.SendRequest(modRequest);
return modResponse.ResultCode;
}
catch (Exception e)
{
Console.WriteLine("\nUnexpected exception occured:\n\t{0}: {1}",
e.GetType().Name, e.Message);
return ResultCode.Unavailable;
}
}
}
}
我知道代码有点笨拙,而且充满了奇怪的 cmets——它是在我开始运行时从 Microsoft 网站上的示例代码剪切、粘贴和修改的。
【问题讨论】:
-
我还想知道为什么您需要 PermissiveModifyControl。你试过没有它吗? stackoverflow.com/q/3450732/1236044
-
您找到问题的答案了吗? (完成一个 C# - redhat LDAP DS,我可能有一个 AD LDAP 项目进来,所以我正在收集一些信息 ;-) 有些人建议如果可能的话,将 AD 服务器上的日志级别提高为“服务器无法处理目录请求”似乎是一般的 AD 错误
标签: c# directoryservices