2

I need to read/write ActiveDirectory User object Terminal Services properties. I tried this:

PrincipalContext context = new PrincipalContext(ContextType.Domain, "CA");

        using (context)
        {
            UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "CA\\vlekovic");
            if (user != null)
            {
                DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
                entry.Properties["msTSProfilePath"].Value = "";
                entry.Properties["msTSHomeDirectory"].Value = "";
                entry.Properties["msTSHomeDrive"].Value = "";
                entry.CommitChanges();
            }

        }

And I tried this:

PrincipalContext context = new PrincipalContext(ContextType.Domain, "CA");

        using (context)
        {
            UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "CA\\vlekovic");
            if (user != null)
            {
                DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
                entry.InvokeSet("msTSProfilePath", "");
                entry.InvokeSet("msTSHomeDirectory", "");
                entry.InvokeSet("msTSHomeDrive", "");
                entry.CommitChanges();
            }

        }

But nothing works.

I tried also with following property names:

  • TerminalServicesProfilePath
  • TerminalServicesHomeDirectory
  • TerminalServicesHomeDrive

But no luck. Any help would be appreciated!

Thanks, Vojin

Vojin
  • 33
  • 4
  • When you say the above code didn't work, can you elaborate? Did you, for example, get an exception? – Nate Jun 06 '13 at 16:53
  • Yes, I get following error: DirectoryServiceCOMExtension was unhandled. The attribute syntax specified to the directory service is invalid. – Vojin Jun 06 '13 at 17:12

2 Answers2

0

If you're on .NET 3.5 and up and using the System.DirectoryServices.AccountManagement (S.DS.AM) namespace, you can easily extend the existing UserPrincipal class to get at more advanced properties, like Manager etc.

Read all about it here:

Basically, you just define a derived class based on UserPrincipal, and then you define your additional properties you want:

[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
    // Implement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context)
    { }

    // Implement the constructor with initialization parameters.    
    public UserPrincipalEx(PrincipalContext context,
                         string samAccountName,
                         string password,
                         bool enabled) : base(context, samAccountName, password, enabled)
    {} 

    // Create the "TermSrvProfilePath" property.    
    [DirectoryProperty("msTSProfilePath")]
    public string TermSrvProfilePath
    {
        get
        {
            if (ExtensionGet("msTSProfilePath").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("msTSProfilePath")[0];
        }
        set { ExtensionSet("msTSProfilePath", value); }
    }
}

Now, you can use the "extended" version of the UserPrincipalEx in your code:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // Search the directory for the new object. 
    UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");

    // you can easily access the TermSrvProfilePath now
    string path = inetPerson.TermSrvProfilePath;
}        

The same way, you can also add the other properties you're looking for.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Error: PrincipalOperationException was caught - The attribute syntax specified to the directory service is invalid. If I change name of property to TerminalServicesProfilePath I get following error: PrincipalOperationException was unhandled: The specified directory service attribute or value does not exist. – Vojin Jun 06 '13 at 17:56
0

I did exactly the same way you told us.

I tried with an other fiel as

    [DirectoryProperty("wWWHomePage")]
    public string wWWHomePage
    {
        get
        {
            if (ExtensionGet("wWWHomePage").Length != 1)
                return null;

            return (string)ExtensionGet("wWWHomePage")[0];

        }
        set { this.ExtensionSet("wWWHomePage", value); }
    }

But with the TermSrvProfilePath , it always return "empty" ...

  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – VP. Jul 21 '15 at 09:21
  • i can't comment his last reply :/ i haven't enought reputation – Baptiste Bauer Jul 21 '15 at 12:51