Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:11:39+00:00 2026-06-18T09:11:39+00:00

I am using a custom class to expose some custom schema in Active Directory

  • 0

I am using a custom class to expose some custom schema in Active Directory. I am storing a binary blob, per the project requirements this data must be stored in the AD, I can not use a external store (I would if I could).

When I create the user it stores the blob fine. I also can retrieve the blob back out fine too and get all my data. The issue is if I need to update the value and I am getting errors

Small example program:

using System;
using System.DirectoryServices.AccountManagement;

namespace SandboxConsole40
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var context = new PrincipalContext(ContextType.Domain))
            {
                using (var clear = ExamplePrincipal.FindByIdentity(context, "example"))
                {
                    if (clear != null)
                        clear.Delete();
                }

                using (var create = new ExamplePrincipal(context, "example", "Password1", false))
                {
                    create.Save();
                }

                using (var set = ExamplePrincipal.FindByIdentity(context, "example"))
                {
                    set.BlobData = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF }; //This fails with method 2.
                    set.Save();
                }

                using (var lookup = ExamplePrincipal.FindByIdentity(context, "example"))
                {
                    Console.WriteLine(BitConverter.ToString(lookup.BlobData));
                }

                using (var update = ExamplePrincipal.FindByIdentity(context, "example"))
                {
                    update.BlobData = new byte[] { 0x12, 0x34, 0x56, 0x67 };
                    update.Save(); //This save fails with method 1.
                }
            }

            Console.WriteLine("Done");
            Console.ReadLine();
        }

        [DirectoryObjectClass("user")]
        [DirectoryRdnPrefix("CN")]
        class ExamplePrincipal : UserPrincipal
        {
            public ExamplePrincipal(PrincipalContext context) : base(context) { }

            public ExamplePrincipal(PrincipalContext context, string samAccountName, string password, bool enabled)
                : base(context, samAccountName, password, enabled) { }

            public static new ExamplePrincipal FindByIdentity(PrincipalContext context, string identityValue)
            {
                return (ExamplePrincipal)FindByIdentityWithType(context, typeof(ExamplePrincipal), identityValue);
            }

            [DirectoryProperty("vwBlobData")]
            public byte[] BlobData
            {
                get
                {
                    if (ExtensionGet("vwBlobData").Length != 1)
                        return null;

                    return (byte[])ExtensionGet("vwBlobData")[0];
                }
                set
                {
                    //method 1
                    this.ExtensionSet("vwBlobData",  value );

                    //method 2
                    //this.ExtensionSet("vwBlobData", new object[] { value});
                }
            }
        }
    }
}

If I use method 1 I get the following exception on the update.Save() operation

System.DirectoryServices.AccountManagement.PrincipalOperationException was unhandled
  HResult=-2146233087
  Message=The specified directory service attribute or value already exists.

  Source=System.DirectoryServices.AccountManagement
  ErrorCode=-2147016691
  StackTrace:
       //Snip
  InnerException: System.DirectoryServices.DirectoryServicesCOMException
       HResult=-2147016691
       Message=The specified directory service attribute or value already exists.

       Source=System.DirectoryServices
       ErrorCode=-2147016691
       ExtendedError=8321
       ExtendedErrorMessage=00002081: AtrErr: DSID-030F154F, #1:
    0: 00002081: DSID-030F154F, problem 1006 (ATT_OR_VALUE_EXISTS), data 0, Att 82818fec (vwBlobData)

       StackTrace:
            //Snip
       InnerException: 

If I use method 2 I get a exception on from the this.ExtensionSet call from the set.BlobData call.

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Collections whose elements are another collection cannot be set by ExtensionClasses.
  Source=System.DirectoryServices.AccountManagement
  StackTrace:
      //Snip
  InnerException: 


In summary: I can set the value if it is currently not set, but if I want to overwrite a existing value I am getting a error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:11:40+00:00Added an answer on June 18, 2026 at 9:11 am

    I found a work around, by setting the value to null first it no-longer throws the exception.

    using (var update = ExamplePrincipal.FindByIdentity(context, "example"))
    {
        update.BlobData = null;
        update.Save();
        update.BlobData = new byte[] { 0x12, 0x34, 0x56, 0x67 };
        update.Save(); //No longer fails with method 1.
    }
    

    I am leaving the question open for a bit to see if anyone else can answer if there is a “proper” way to do this.


    Found a 2nd work around that does not require forcing a save.

    [DirectoryProperty("vwBlobData")]
    public byte[] BlobData
    {
        get
        {
            if (ExtensionGet("vwBlobData").Length != 1)
                return null;
    
            return (byte[])ExtensionGet("vwBlobData")[0];
        }
        set
        {
            ((DirectoryEntry)this.GetUnderlyingObject())
                                 .Properties["vwBlobData"].Value = value;
        }
    }
    

    By casting directly to the underlying object you can set the value directly. I checked using ILSpy and disposing the AccountManagement wrapper disposes the underlying object, so no Dispose() is required for the GetUnderlyingObject() call.


    BEST SOLUTION

    I found out the 2nd work around required the object to be persisted to work, so I made a best of both worlds approach. This works when you have not yet persisted the object, when the object is null, and when the object has a value already.

    [DirectoryProperty("vwBlobData")]
    public byte[] BlobData
    {
        get
        {
            if (ExtensionGet("vwBlobData").Length != 1)
                return null;
    
            return (byte[])ExtensionGet("vwBlobData")[0];
        }
        set
        {
            if(ExtensionGet("vwBlobData").Length == 0)
                this.ExtensionSet("vwBlobData", data); 
            else
                ((DirectoryEntry)this.GetUnderlyingObject())
                                     .Properties["vwBlobData"].Value = data;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a custom class to display some info on a table view.
This question is kind of the next level of F# Set using custom class
I'm using my custom class that describes some states and values: class MyClass {
I am using an Translator object (custom class) to expose website texts (the object
I'm using a custom class to store information about files found in a directory
I'm using a custom class for my UIViews, which I am controlling some configuration
I'm using custom class that subclass from UILabel. It works fine unless I change
I am using a custom class as the delegate and datasource on a UITableView.
I'm using a custom class that extends JFrame, but sometimes it shows nothing. I
I am having problems using my custom class with a std::map. The class dynamically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.