我正在创建一个应用程序 (.Net 3.5),以允许用户在 Active Directory 中更新自己的照片和电话号码。
我正在使用 UserPrincipal 类,我已使用此 example 对其进行了扩展
// Create the "thumbnailPhoto" property.
[DirectoryProperty("thumbnailPhoto")]
public byte[] thumbnailPhoto
{
get
{
if (ExtensionGet("thumbnailPhoto").Length != 1)
return null;
return (byte[])ExtensionGet("thumbnailPhoto")[0];
}
set
{
ExtensionSet("thumbnailPhoto", value);
}
}
我获取字节数组并将其写入图片框
pictureBoxthumbnail.Image = Image.FromStream(new MemoryStream(userPrincipal.thumbnailPhoto));
这显示了表单上的图片,到目前为止一切顺利。当我尝试将图像写入 Active Directory 时,我将 pictureBox 转换为字节数组
userPrincipal.thumbnailPhoto = ImageManipulation.imageToByteArray(pictureBoxthumbnail.Image);
public static byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
并尝试保存数据,但出现以下异常。
System.DirectoryServices.AccountManagement.PrincipalOperationException was unhandled
Message=Unspecified error
Source=System.DirectoryServices.AccountManagement
ErrorCode=-2147467259
我怀疑我的图片框到字节数组是错误的。有人可以帮忙吗?
谢谢。
最佳答案
我今天设法使用下面的函数使其工作。希望这对其他人有帮助。
private void UpdatePhoto()
{
var principalContext = new PrincipalContext(ContextType.Domain);
var userPrincipal = UserPrincipalEx.FindByIdentity(principalContext, System.Environment.UserName);
DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("LDAP://{0}:389/{1}", principalContext.ConnectedServer, userPrincipal.DistinguishedName));
directoryEntry.Properties["thumbnailPhoto"].Value = ImageManipulation.imageToByteArray(pictureBoxthumbnail.Image);
directoryEntry.CommitChanges();
}
关于c# - 将thumbnailPhoto 属性写入Active Directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912517/