web-dev-qa-db-fra.com

Comment vérifier si un utilisateur appartient à un groupe AD?

Au début, j’imaginais que le code ci-dessous fonctionnait, car si j’ai le groupe comme "informatique", il fonctionne correctement car mon nom d’utilisateur se trouve dans le groupe informatique d’Active Directory. Ce que j’ai appris, c’est qu’il retourne toujours vrai si j’ai ou non mon nom d’utilisateur dans le groupe informatique et si je le change en un autre groupe, j’ai toujours un faux. Toute aide serait appréciée.

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // tab control security for admin tab
        bool admin = checkGroup("IT");

        if ((admin == true) && (tabControl1.SelectedTab == tpHistory))
        {
            tabControl1.SelectedTab = tpHistory;
        }
        else if ((admin == false) && (tabControl1.SelectedTab == tpHistory))
        {
            tabControl1.SelectedTab = tpRequests;
            MessageBox.Show("Unable to load tab. You have insufficient privileges.",
                "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }

    // check Active Directory to see if user is in Marketing department group
    private static bool checkGroup(string group)
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(group);
    }
50
Sealer_05

Puisque vous êtes sur .NET 3.5 et plus, vous devriez jeter un oeil à la System.DirectoryServices.AccountManagement _ (S.DS.AM) espace de noms. Lisez tout a propos de ça ici:

Fondamentalement, vous pouvez définir un contexte de domaine et rechercher facilement des utilisateurs et/ou des groupes dans AD:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME");

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

if(user != null)
{
   // check if user is member of that group
   if (user.IsMemberOf(group))
   {
     // do something.....
   } 
}

Le nouveau S.DS.AM facilite grandement les interactions avec les utilisateurs et les groupes dans AD!

101
marc_s

Léger écart par rapport à l'exemple @marc_s, implémenté dans la méthode static void Main() dans Program:

DomainCtx = new PrincipalContext( ContextType.Domain , Environment.UserDomainName );
if ( DomainCtx != null ) {
    User = UserPrincipal.FindByIdentity( DomainCtx , Environment.UserName );
}

DomainCtx et User sont deux propriétés statiques déclarées sous Program

Ensuite, sous d'autres formes, je fais simplement quelque chose comme ceci:

if ( Program.User.IsMemberOf(GroupPrincipal.FindByIdentity(Program.DomainCtx, "IT-All") )) {
    //Enable certain Form Buttons and objects for IT Users

}
12
GoldBishop

Vous ne pouvez pas le faire de cette façon. Vous devez interroger Active Directory. Vous pouvez utiliser un wrapper pour AD. Départ http://www.codeproject.com/Articles/10301/Wrapper-API-for-using-Microsoft-Active-Directory-S

0
İbrahim ULUDAĞ