我正在尝试将 a 添加Contact
到分发列表中。
这是我的做法:
public void AddContactsToGroup(string groupName, string[] userNames)
{
using (PrincipalContext context = GetPrincipalContext())
{
var group = GroupPrincipal.FindByIdentity(context, IdentityType.Name, groupName);
if (group.IsSecurityGroup.HasValue && group.IsSecurityGroup.Value == true)
throw new ArgumentException("Groups cannot be security groups.");
foreach (var userName in userNames)
{
var query = new ContactPrincipal(context)
{
Name = userName
};
var user = Search(query).First();
group.Members.Add(user);
}
group.Save(context);
}
}
private List<T> Search<T>(T query) where T : Principal
{
var searcher = new PrincipalSearcher();
searcher.QueryFilter = query;
PrincipalSearchResult<Principal> results = searcher.FindAll();
return results.Cast<T>().ToList();
}
这是ContactPrincipal
课程(我从某个地方借来的):
[DirectoryObjectClass("contact")]
[DirectoryRdnPrefix("CN")]
public class ContactPrincipal : AuthenticablePrincipal
{
public ContactPrincipal(PrincipalContext context)
: base(context)
{
}
public static ContactPrincipal FindByIdentity(PrincipalContext context, string identityValue)
{
return (ContactPrincipal) Principal.FindByIdentityWithType(context, typeof (ContactPrincipal), identityValue);
}
public static ContactPrincipal FindByIdentity(PrincipalContext context, IdentityType identityType,
string identityValue)
{
return
(ContactPrincipal)
Principal.FindByIdentityWithType(context, typeof (ContactPrincipal), identityType, identityValue);
}
[DirectoryProperty("mail")]
public string EmailAddress
{
get
{
if (ExtensionGet("mail").Length == 1)
{
return ExtensionGet("mail")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("mail", value); }
}
[DirectoryProperty("givenName")]
public string GivenName
{
get
{
if (ExtensionGet("givenName").Length == 1)
{
return ExtensionGet("givenName")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("givenName", value); }
}
[DirectoryProperty("middleName")]
public string MiddleName
{
get
{
if (ExtensionGet("middleName").Length == 1)
{
return ExtensionGet("middleName")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("middleName", value); }
}
[DirectoryProperty("sn")]
public string Surname
{
get
{
if (ExtensionGet("sn").Length == 1)
{
return ExtensionGet("sn")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("sn", value); }
}
[DirectoryProperty("mobile")]
public string MobileTelephoneNumber
{
get
{
if (ExtensionGet("mobile").Length == 1)
{
return ExtensionGet("mobile")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("mobile", value); }
}
[DirectoryProperty("telephoneNumber")]
public string VoiceTelephoneNumber
{
get
{
if (ExtensionGet("telephoneNumber").Length == 1)
{
return ExtensionGet("telephoneNumber")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("telephoneNumber", value); }
}
}
我的问题是,当我点击
group.Members.Add(user)
方法中的行AddContactsToGroup
,抛出一个错误,指出
Principal 对象必须具有有效的 SID IdentityType 才能执行此操作。
当我询问 的属性时ContactPrincipal
,Sid
实际上是 null。这并不奇怪,因为 aContact
是一个没有安全性的对象。
如何添加Contact
到非安全组?