更新Active Directory缩略图时出现“指定的目录服务属性或值已存在”错误

时间:2012-12-20 15:39:42

标签: c# active-directory userprincipal

我要从c#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;
    }

一些谷歌搜索建议我需要先清除缩略图属性。尝试将其设置为null,如上所述,但没有快乐。

编辑:我之前遗漏的添加方法

  // 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;

    }

1 个答案:

答案 0 :(得分:-1)

我不熟悉UserPrincipalEx,但我猜你必须在将缩略图设置为null后才能调用Save函数,以便在服务器上实际清除它。