13
PrincipalContext context = new PrincipalContext(ContextType.Domain, "ipofmachine", "DC=xyz,DC=org", "username", "Password");

UserPrincipal userPrinciple = UserPrincipal.FindByIdentity(context, "User0"); 
var groups = userPrinciple.GetAuthorizationGroups();

if (userPrinciple != null)
{
    foreach (GroupPrincipal gp in groups)
    {
        //some thing
    }
}

Is there any permission that I need to give? In some of the blogs I learned that if there are no users which are set to include the SID history then this will work fine (but i think you can not edit the sid values of the groups)

1
  • 4
    Posting the exception would be a good place to start.
    – Sam Axe
    Apr 25, 2013 at 18:34

1 Answer 1

16

I've found there's an issue when you add a domain user to a local group, but later that domain user is deleted out of Active Directory. The state of that local group is that instead of a domain username showing up as a member, the SID is used instead.

BUT!

That SID doesn't exist in Active Directory anymore causing things to go boom.

Of course there could be many other reasons for an NoMatchingPrincipalException to pop up, so this code provides a workaround for that. It comes from a terrific post on MSDN. The code below is a modified version found here:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/9dd81553-3539-4281-addd-3eb75e6e4d5d/getauthorizationgroups-fails-with-nomatchingprincipalexception

    public static IEnumerable<Principal> getAuthorizationGroups(UserPrincipal user)
    {
        PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
        List<Principal> ret = new List<Principal>();
        var iterGroup = groups.GetEnumerator();
        using (iterGroup)
        {
            while (iterGroup.MoveNext())
            {
                try
                {
                    Principal p = iterGroup.Current;
                    Console.WriteLine(p.Name);
                    ret.Add(p);
                }
                catch (NoMatchingPrincipalException pex)
                {
                    continue;
                }
            }
        }
        return ret;
    }
3
  • 1
    You noticed that very well. Great solution!
    – S.Pols
    Jun 12, 2015 at 12:10
  • 1
    @S.Pols Thanks, That was a bear to track down. I think eventually, I ended up stripping this out and using WindowsPrincipal.IsInRole(string). But glad this solution helps. Querying Active Directory can be really slow in some environments
    – C. Tewalt
    Jun 12, 2015 at 18:39
  • 1
    It can also be migrated users, can't find out why, but migrated users have their old username/sid as an group when using the GetAuthorizationGroups metod. to find if that is the problem, run this command in ps: Get-ADUser -Filter { samaccountname -eq "USER" } -Properties sidhistory
    – neslekkiM
    Oct 5, 2018 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.