2

In my .NET 2.0 C# applcation I need to determine if a user (with password) has ability to modify (write) option in Active Directory. I hope there is a way using DirectoryEntry without creating and then deleting new object in AD.

Thank you for your help.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Dafko
  • 23
  • 1
  • 3

2 Answers2

10

Like Olive said, it's difficult to do it right yourself. It's difficult to do right because the permissions can be passed onto your user account via Active Directory groups. So, in order to find out the effective permission for a particular user account, you have to find out all the groups the user belongs to.

Fortunately, Active Directory has a special type of attributes called constructed attributes. By default, if you are using AD Explorer or ADSI Edit to browse your object's, these kinds of attributes are not shown. In ADSI Edit, you can set the Filter to include these constructed attributes. One of the useful constructed attributes here is allowedAttributesEffective. It's a multi-value attribute and it contains all attributes that your current user has permission to write to. It's calculated by Active Directory on the fly. It takes care all the inheritance, deny override and group permissions. If you have permission to write to cn attribute, you will see cn as one of the values in it.

Here is a sample for checking a particular user has write permissions on a particular sets of attributes on a specific object on Active Directory.

static bool CheckWritePermission(string path, string username, string password, string[] properties)
{
    using (DirectoryEntry de = new DirectoryEntry(path, username, password))
    {
        de.RefreshCache(new string[] {"allowedAttributesEffective"});
        return properties.All( property => de.Properties["allowedAttributesEffective"].Contains(property));
    }
}

Yes, it's not exactly what you want. You are asking to check if a user has WriteAllProperties permission. Actually, WriteAllProperties permission is a collection of write property permissions on different attributes. You may need to do some homework to find out what attributes your application really cares. Then, just pass in those attributes.

If you really have no idea what attributes to check, this one should be good enough

static bool CheckWritePermission(string path, string username, string password)
{
    using (DirectoryEntry de = new DirectoryEntry(path, username, password))
    {
        de.RefreshCache(new string[] { "allowedAttributesEffective" });
        return de.Properties["allowedAttributesEffective"].Value != null;
    }            
}

Here, I am checking if the returned allowedAttributesEffective is null or not. If null, it means it doesn't have any permissions to write to any attributes. I am assuming your administrator would either grant all write properties permission or deny all write properties. I think this is a valid assumption in most cases.

Harvey Kwok
  • 11,713
  • 6
  • 37
  • 59
  • This reads very promising. Will see when i have time to check if this will work also for my problem. But if it really works as you described i will *love* this feature. ;-) – Oliver Feb 09 '11 at 08:07
  • Thank you Harvey, this was exactly what I was looking for. As you wrote there are just two groups of users. Masters with whole access and users with read-only access. Using the allowedAttributesEffective property of DirectoryEntry I was able to determinite between this groups. ;-) – Dafko Feb 10 '11 at 10:51
0

As you can see in my question, there seems no possibility to simply find out the rights of a random user to a specfic object within the AD.

If anyone knows for a simple way, please let me know.

Community
  • 1
  • 1
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Sounds like you should have voted to close as a duplicate, rather than posting an answer. No reason for people to let you know *here* instead of on the question you've already asked. – Cody Gray - on strike Feb 08 '11 at 11:20
  • @Cody: I don't think it is a real duplicate, cause here is the question if a user has write access at a specific location. In my question i want to know all write access places for a specific user. But both questions have the same root problem of lacking support for the needed functionality. – Oliver Feb 08 '11 at 12:19
  • @Harvey: Yes, it seems to work. :-) I put an answer to my own question, adding credits to you into it and upvoted your answer above. – Oliver Feb 11 '11 at 09:52