检查用户是否在AD组中并返回复选框 [英] Check If User is in AD groups and return to check boxes

查看:114
本文介绍了检查用户是否在AD组中并返回复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我需要检查用户是否属于AD组,并将每个单词返回到复选框

Hello. I need to check if user belongs to groups AD and return each single to checkboxes

我只能检查一个组。如何做到这一点?

我试过:

GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx," #Print_Color,#Secondy,#Third");

但有错误:参数2:无法从'string'转换为'System.DirectoryServices.AccountManagement.IdentityType'

I can only check one group. How to do it in few?
I tried:
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "#Print_Color, #Second, #Third");
but have error: Argument 2: cannot convert from 'string' to 'System.DirectoryServices.AccountManagement.IdentityType'

            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain");
            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Login);
            GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "#Print_Color", "Od");

            if (user != null)
            {
                if (user.IsMemberOf(group))
                {
                    CheckBox_Uzytkownik_WydrukKolor.IsChecked = true;
                }
            }
            else
            {
                var window = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault();
                if (window != null)
                    await window.ShowMessageAsync("Error", "test");
                return;
            }

推荐答案

您应该以另一种方式解决这个问题 - 获取用户所属的所有组,然后比较它们。

You should probably go about this the other way - get all the groups a user is a member of and then compare them.

//Converted from Powershell
IEnumerable<Principal> GetUserGroups ( string domainName, string username )
{
   var context = new PrincipalContext(ContextType.Domain, domainName);
   var account = UserPrincipal::FindByIdentity(context, username);
   if (account != null)
      return account.GetAuthorizationGroups();

   return Enumerable.Empty<Principal>();
}


这篇关于检查用户是否在AD组中并返回复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆