System.DirectoryServices.Protocol名前空間のグループからユーザーを追加削除したい。
私はここに言及されたサンプルを持っています:
しかし、S.DS.Pを使用してユーザーをグループに追加およびグループから削除する方法の例を見つけることができません。
この操作のサンプルを知っている人はいますか?
ありがとう、
Cal-
System.DirectoryServices.Protocol名前空間のグループからユーザーを追加削除したい。
私はここに言及されたサンプルを持っています:
しかし、S.DS.Pを使用してユーザーをグループに追加およびグループから削除する方法の例を見つけることができません。
この操作のサンプルを知っている人はいますか?
ありがとう、
Cal-
属性でを送信ModifyRequest
したいだけです。渡す値は、グループに追加するユーザーの DN です。member
DirectoryAttributeOperation.Add
オブジェクトをグループに追加する例を次に示します。
public static void AddObjectToGroup(string objectName, string groupName)
{
// var credential = new NetworkCredential();
// var identifier = new LdapDirectoryIdentifier("");
using (var connection = new LdapConnection("company.com"))
{
connection.SessionOptions.StartTransportLayerSecurity(null);
connection.Bind();
string objectDn = null;
var request = new SearchRequest("DC=company,DC=com", $"Name={objectName}", SearchScope.Subtree, "distinguishedName");
var response = (SearchResponse)connection.SendRequest(request);
if (response != null)
{
var enumerator = response.Entries.GetEnumerator();
enumerator.Reset();
if (enumerator.MoveNext() && enumerator.Current is SearchResultEntry entry)
{
objectDn = entry.Attributes["distinguishedName"].GetValues(typeof(string)).Select(x => (string)x).FirstOrDefault();
}
}
Log.Information($"object DN: {objectDn}");
string groupDn = null;
request = new SearchRequest("DC=company,DC=com", $"Name={groupName}", SearchScope.Subtree, "distinguishedName");
response = (SearchResponse)connection.SendRequest(request);
if (response != null)
{
var enumerator = response.Entries.GetEnumerator();
enumerator.Reset();
if (enumerator.MoveNext() && enumerator.Current is SearchResultEntry entry)
{
groupDn = entry.Attributes["distinguishedName"].GetValues(typeof(string)).Select(x => (string)x).FirstOrDefault();
}
}
Log.Information($"group DN: {groupDn}");
request = new SearchRequest("DC=company,DC=com", $"(&(objectCategory=Group)(distinguishedName={groupDn}))", SearchScope.Subtree);
response = (SearchResponse)connection.SendRequest(request);
if (response != null && !string.IsNullOrWhiteSpace(objectDn))
{
var members = response.Entries[0].Attributes["member"];
var existing = new List<string>();
for (int i = 0; i < members.Count; i++)
{
existing.Add(members[i].ToString());
}
if (existing.Contains(objectDn)) return;
var dam = new DirectoryAttributeModification { Name = "member" };
dam.Add(objectDn);
dam.Operation = DirectoryAttributeOperation.Add;
var mr = new ModifyRequest(groupDn, dam);
var dr = connection.SendRequest(mr);
Log.Information($"Add Response: {dr.ResultCode.ToString()}");
}
}
}