示例#1
0
        public static bool UserInGroup(string sid, params AdGroup[] groups)
        {
            using (WindowsImpersonationContextFacade impersonationContext
                       = new WindowsImpersonationContextFacade(
                             nc))
            {
                var context       = new PrincipalContext(ContextType.Domain);
                var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.Sid, sid);

                if (userPrincipal == null)
                {
                    return(false);
                }
                ////if (userPrincipal.IsMemberOf(context, IdentityType.Sid, AdUserGroup.GetSidByAdGroup(AdGroup.SuperAdmin))) { return true; }//Если юзер Суперадмин

                foreach (var grp in groups)
                {
                    if (userPrincipal.IsMemberOf(context, IdentityType.Sid, AdUserGroup.GetSidByAdGroup(grp)))
                    {
                        return(true);
                    }
                }


                return(false);
            }
        }
示例#2
0
        public static IEnumerable <KeyValuePair <string, string> > GetSpecialistList(AdGroup grp)
        {
            var list = new Dictionary <string, string>();

            using (WindowsImpersonationContextFacade impersonationContext
                       = new WindowsImpersonationContextFacade(
                             nc))
            {
                var domain = new PrincipalContext(ContextType.Domain);
                var group  = GroupPrincipal.FindByIdentity(domain, IdentityType.Sid, AdUserGroup.GetSidByAdGroup(grp));
                if (group != null)
                {
                    var members = group.GetMembers(true);
                    foreach (var principal in members)
                    {
                        var userPrincipal = UserPrincipal.FindByIdentity(domain, principal.SamAccountName);
                        if (userPrincipal != null)
                        {
                            var name = MainHelper.ShortName(userPrincipal.DisplayName);
                            var sid  = userPrincipal.Sid.Value;
                            list.Add(sid, name);
                        }
                    }
                }

                return(list.OrderBy(x => x.Value));
            }
        }