C#でAD属性(今のところ単一値の文字列属性のみ)を更新するユーティリティメソッドを作成しようとしています。これは、IISに依存しないスタンドアロンユーティリティです。この方法は、HRシステムからADにデータをロードするために使用されます。
System.DirectoryServices.Protocolsを使用して、オブジェクトと属性を効果的に読み取ることができます。しかし、ModifyRequestメソッドを呼び出すと、「サーバーはディレクトリ要求を処理できません」というメッセージとともにDirectoryOperationExceptionが発生します。
別のStackOverflowの質問に基づく: .Netのディレクトリサービスは奇妙な例外をスローします
SSL LDAPにポート636を使用しようとしましたが、動作は変わりません。
私はIISを使用しておらず、.NET 4.5を使用しているため、.NET/IIS用のMicrosoftパッチを適用しないでください。
これをグーグルで検索しても効果はありません。
このエラーが発生する理由とその修正方法を知っていれば、非常にありがたいです。
以下のコード..Connには、囲んでいるユーティリティクラスからの有効で認証されたLDAP接続が含まれていると想定してください。必要に応じて、囲んでいるユーティリティクラスの完全なソースを提供できます。
例外は次のSendRequest
行で発生しますModifyStringAttributeValues
:
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;
}
}
}
}
コードが少し不格好で、奇妙なコメントでいっぱいであることを私は知っています-私がそれを動作させている間、それはマイクロソフトのサイトのサンプルコードから切り取られ、貼り付けられ、そして修正されます。