我正在尝试以编程方式从 ac# webservice 更新各种 AD 属性。
我可以更新电话/办公室/标题等没问题,但是当我尝试更新 ThumbnailPhoto 时出现错误:指定的目录服务属性或值已存在
我正在通过扩展类进行更新(根据Get job title using System.DirectoryServices.AccountManagement)
我用来更新 AD 记录的代码是:
public ADRecord UpdateADRecord(string UserName)
{
DeltekRecord dr = GetDeltekRecord(UserName);
UserPrincipalEx upx = GetUser(UserName);
upx.Office = GetFriendlyFieldName(dr.Office);
upx.MobilePhone = dr.MobilePhone;
upx.TelephoneNumber = dr.WorkPhone;
upx.Department = GetFriendlyFieldName(dr.BusinessUnit);
upx.Title = dr.Title;
// Handle Company
string Company = "";
if ((dr.Org ?? "").Contains(":FOO:"))
Company = "Foo";
else
Company = "Bar";
upx.Company = Company;
// Handle Home Phone
string HomeNo = "";
if ((dr.WorkPhone.Length > 0) && (dr.Office.Length > 0))
{
switch (dr.Office.ToLower()) {
case "washington":
HomeNo = "8" + dr.WorkPhone.Substring(dr.WorkPhone.Length - 3, 3);
break;
case "ohio":
HomeNo = "9" + dr.WorkPhone.Substring(dr.WorkPhone.Length - 3, 3);
break;
case "nooyoik":
HomeNo = "2" + dr.WorkPhone.Substring(dr.WorkPhone.Length - 3, 3);
break;
default:
HomeNo = "";
break;
}
}
upx.Thumbnail = null;
upx.Thumbnail = GetThumbnail(dr.Employee);
upx.Save();
ADRecord adr = GetADRecord(UserName);
return adr;
}
一些谷歌搜索建议我需要先清除缩略图属性。如上所述,已尝试将其设置为空,但没有任何乐趣。
编辑:添加我之前错过的方法
// Get the relevant details from AD
[WebMethod]
public ADRecord GetADRecord(string userName)
{
ADRecord adr = new ADRecord();
PrincipalContext oPrincipalContext = GetPrincipalContext();
// Search the directory for the new object.
UserPrincipalEx myUser = UserPrincipalEx.FindByIdentity(oPrincipalContext, userName);
if (myUser != null)
{
adr.Company = myUser.Company;
adr.Department = myUser.Department;
adr.HomePhone = myUser.HomePhone;
adr.MobilePhone = myUser.MobilePhone;
adr.Office = myUser.Office;
adr.TelephoneNumber = myUser.TelephoneNumber;
adr.ThumbNail = myUser.Thumbnail;
adr.Title = myUser.Title;
}
return adr;
}
// Get the user's thumbnail
[WebMethod]
public byte[] GetThumbnail(int EmpNo)
{
System.Net.WebClient wclient = new System.Net.WebClient();
wclient.Credentials = new System.Net.NetworkCredential("user","pass", "domain");
string url = "http://myurl.mycompany.com/PeopleEmployeePhoto.ashx?Employee=" + EmpNo;
byte[] imageData;
using (wclient)
{
imageData = wclient.DownloadData(new Uri(url));
}
return imageData;
}