how mapping group users?

Sep 7, 2012 at 2:45 AM

code:

[DirectoryType("group")]
    public class Group : EntryObject
    {
        [DirectoryProperty("samaccountname")]
        public string Name { getset; }
 
        [EntryCollectionProperty("member", IsReferenceCollection = true)]
        public EntryCollection<User> Users
        {
            get
            {
                return ((IEntryWithRelationships)this).RelationshipManager.GetEntryCollection<User>("Users");
            }
        }
    }
but it doesn't work
Coordinator
Sep 10, 2012 at 7:03 PM
Edited Sep 10, 2012 at 7:04 PM

@cdjboy

Sorry for the delayed response, I have been on vacation. The below code loads the users for the group...but, please not that even microsoft has a warning about using this approach, but, it is the only way to pull ALL users associated with the given group.

 

[DirectoryType("group")]
public class Group : EntryObject
{
	[DirectoryProperty("samaccountname")]
	public string Name { get; set; }

	[EntryCollectionProperty("memberOf", MatchingRule = MatchingRuleType.InChain)]
	public EntryCollection<User> Users
	{
		get
		{
			return ((IEntryWithRelationships)this).RelationshipManager.GetEntryCollection<User>("Users");
		}
	}
}

 

On the MatchingRule  property of EntryCollectionPropertyAttribute, I have specified the warning microsoft has in the xml comments for the property.

"This rule is limited to filters that apply to the DN. This isa special "extended match operator that walks the chain ofancestry in objects all the way to the root until it finds a match.NOTE: Some such queries on subtrees may be more processor intensive,such as chasing links with a high fan-out; that is, listing all thegroups that a user is a member of. "

See http://msdn.microsoft.com/en-us/library/windows/desktop/aa746475(v=vs.85).aspx for more information.

Hope this helps,

- Stephen

Sep 11, 2012 at 6:28 AM

but it still not work.

error message:

Invalid cast from 'System.String' to 'System.DirectoryServices.Linq.EntryObjects.EntryCollection`1[[SQLDataCompare.User, SQLDataCompare, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.

Coordinator
Sep 11, 2012 at 9:10 PM
Edited Sep 12, 2012 at 4:29 PM

Are you trying to access, Group.Users in a query? If so, I haven't figured out a good way to support that. Right now, it only supports the following example.

var group = context.Groups.First(g => g.Name == "SomeGroupName");
var user = group.Users.FirstOrDefault(u => u.FirstName == "Stephen");

Unfortunately LDAP doesn't support joins / sub-queries, so, there isn't a way for me to handle creating the LDAP filter from the a complex expression tree. See the following example.

var groups = context.Groups.Where(g => g.Users.First(u => u.UserName == "sbaker") != null);

But, if you aren't doing anything like the above, then please post as much of your code as possible and I'll see if I can troubleshoot the issue.

- Stephen

Marked as answer by sbaker on 10/7/2013 at 9:44 AM
Jan 26, 2013 at 8:41 PM
Edited Jan 28, 2013 at 1:16 AM

I am also having trouble with getting user results from my group object and vice versa when trying to get groups from a user object.

Is there something else I can try, I have used most of your code from Documentation and I am also trying to run the code in the above comment.

Is this maybe a wrong OU misconfiguration on my part for the DirectoryType attribute?

PS. Loving the ease of retrieving users and groups from AD so far using Linq. Thanks for you hard work on this.

 

*Update

I figured out my issue.  My LDAP connection string was wrong.  I am now getting results when retrieving groups for users and also getting users for groups.

Coordinator
Jan 28, 2013 at 5:19 PM

@aknight,

Thanks, I'm glad you are finding the framework useful. I use it at work and it works great. I'm glad you found the issue. Please let me know if I can be of any help.

- Stephen