Error with UserPrincipal.GetAuthorizationGroups() method

前端 未结 3 1386
半阙折子戏
半阙折子戏 2021-02-08 21:42

I am having an issue using the GetAuthorizationGroups method of the UserPrincipal class in a web application.

Using the following code, I am receiving \"While trying to

3条回答
  •  我在风中等你
    2021-02-08 22:15

    I dealt with this same problem. See discussion on similar question. https://stackoverflow.com/a/8347817/2012977

    Solution is below:

    public List GetGroups(string userName)
        {
            var result = new List();
            PrincipalContext ctx = GetContext(); /*function to get domain context*/
            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);
            if (user != null)
            {
                PrincipalSearchResult groups = user.GetAuthorizationGroups();
    
                var iterGroup = groups.GetEnumerator();
                using (iterGroup)
                {
                    while (iterGroup.MoveNext())
                    {
                        try
                        {
                            Principal p = iterGroup.Current;
                            result.Add((GroupPrincipal) p);
                        }
                        catch (PrincipalOperationException)
                        {
                            continue;
                        }
                    }
                }
            }
    
            return result;
        }
    

提交回复
热议问题