我怎样才能让一个用户,这是否是一个组或子组在广告中recursiv搜索?是一个、这是、用户、广告

2023-09-09 21:42:31 作者:闻香识女人

您好我使用的ASP.NET应用程序中的活动目录和C#,我想,我得到一个布尔值,如果用户是在一个集团或本群。我写的让我的方法个用户是否是该组中,但不是在这个子组:(

我怎样才能让一个recursiv搜索在我的方法:

我在这里的code:

 公共静态布尔IsUserInGroup(直流串,串用户,弦乐群)
        {
            PrincipalContext CTX =新PrincipalContext(ContextType.Domain,DC);

            GroupPrincipal P = GroupPrincipal.FindByIdentity(CTX,组);

            UserPrincipal U = UserPrincipal.FindByIdentity(CTX,IdentityType.SamAccountName,用户);

            布尔isMember = u.IsMemberOf(对);

            返回isMember;
        }

静态无效的主要(字串[] args)
        {
            字符串DC =company.com;
            字符串用户=test.w;

            布尔isadmin = IsUserInGroup(DC,用户,TAdmin);
            布尔isUser = IsUserInGroup(DC,用户,TUSER);

            Console.WriteLine(管理员:+ isadmin);
            Console.WriteLine(用户:+ isUser);

            到Console.ReadLine();

        }
 

解决方案

而不是 IsMemberOf 的你应该使用方法 GetMembers(布尔)与真。它将返回该组的所有成员 - 甚至嵌套。然后做一个循环来检查,如果你的用户的原则是结果。检查这的链接。

附加说明:尝试这样的code

 公共静态布尔IsUserInGroup(直流串,串用户,弦乐群)
{
    布尔发现= FALSE;

    PrincipalContext CTX =新PrincipalContext(ContextType.Domain,DC);
    GroupPrincipal P = GroupPrincipal.FindByIdentity(CTX,组);
    UserPrincipal U = UserPrincipal.FindByIdentity(CTX,IdentityType.SamAccountName,用户);

    发现= p.GetMembers(真)。载(U);

    p.Dispose();
    u.Dispose();

    返回发现;
}
 

陪了大家20多年的技术要被干掉了,你以后看到的广告会变少吗

Hi I use the Active Directory and C# in a ASP.NET Application and I want that I get a bool if a User is in a Group or in this SubGroups. I have write a method that get me whether th user is in the group but not in this Subgroups :(

How I can make a recursiv search in my method:

here my code:

public static bool IsUserInGroup(string dc, string User, string group) 
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);

            GroupPrincipal p = GroupPrincipal.FindByIdentity(ctx, group);

            UserPrincipal u = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, User);

            bool isMember = u.IsMemberOf(p); 

            return isMember; 
        }

static void Main(string[] args)
        {
            string dc = "company.com";
            string user = "test.w";

            bool isadmin = IsUserInGroup(dc, user, "TAdmin");
            bool isUser = IsUserInGroup(dc, user, "TUser");

            Console.WriteLine("Admin: " + isadmin);
            Console.WriteLine("User: " + isUser);

            Console.ReadLine();

        }

解决方案

Instead of IsMemberOf method you should use GetMembers(Boolean) with 'true'. It will return all the members of the group - even nested. Then make a loop to check if your user principle is in the result. Check this link.

Additional note: try such code

public static bool IsUserInGroup(string dc, string User, string group) 
{
    bool found = false;

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);
    GroupPrincipal p = GroupPrincipal.FindByIdentity(ctx, group);
    UserPrincipal u = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, User);

    found = p.GetMembers(true).Contains(u);

    p.Dispose();
    u.Dispose();

    return found; 
}