Leggere in inglese

Condividi tramite


Semaphore.GetAccessControl Metodo

Definizione

Ottiene la sicurezza del controllo di accesso per un semaforo di sistema denominato.

C#
public System.Security.AccessControl.SemaphoreSecurity GetAccessControl();

Restituisce

Oggetto SemaphoreSecurity che rappresenta la sicurezza del controllo di accesso per il semaforo di sistema denominato.

Eccezioni

L'oggetto Semaphore corrente rappresenta un semaforo di sistema denominato e l'utente non dispone di diritti ReadPermissions.

-oppure-

L'oggetto Semaphore corrente rappresenta un semaforo di sistema denominato e non è stato aperto con diritti ReadPermissions.

Esempio

Nell'esempio di codice seguente viene illustrato il comportamento tra processi di un semaforo denominato con sicurezza del controllo di accesso. Nell'esempio viene utilizzato l'overload del OpenExisting(String) metodo per verificare l'esistenza di un semaforo denominato.

Se il semaforo non esiste, viene creato con un numero massimo di due e con la sicurezza del controllo di accesso che nega all'utente corrente il diritto di usare il semaforo, ma concede il diritto di leggere e modificare le autorizzazioni per il semaforo.

Se si esegue l'esempio compilato da due finestre di comando, la seconda copia genererà un'eccezione di violazione di accesso nella chiamata al OpenExisting(String) metodo . L'eccezione viene intercettata e l'esempio usa l'overload del OpenExisting(String, SemaphoreRights) metodo per aprire il semaforo con i diritti necessari per leggere e modificare le autorizzazioni. La sicurezza del controllo di accesso per il semaforo di sistema viene ottenuta usando il GetAccessControl metodo .

Dopo aver modificato le autorizzazioni, il semaforo viene aperto con i diritti necessari per immettere e rilasciare. Se si esegue l'esempio compilato da una terza finestra di comando, viene eseguito usando le nuove autorizzazioni.

C#
using System;
using System.Threading;
using System.Security.AccessControl;

internal class Example
{
    internal static void Main()
    {
        const string semaphoreName = "SemaphoreExample5";

        Semaphore sem = null;
        bool doesNotExist = false;
        bool unauthorized = false;

        // Attempt to open the named semaphore.
        try
        {
            // Open the semaphore with (SemaphoreRights.Synchronize
            // | SemaphoreRights.Modify), to enter and release the
            // named semaphore.
            //
            sem = Semaphore.OpenExisting(semaphoreName);
        }
        catch(WaitHandleCannotBeOpenedException)
        {
            Console.WriteLine("Semaphore does not exist.");
            doesNotExist = true;
        }
        catch(UnauthorizedAccessException ex)
        {
            Console.WriteLine("Unauthorized access: {0}", ex.Message);
            unauthorized = true;
        }

        // There are three cases: (1) The semaphore does not exist.
        // (2) The semaphore exists, but the current user doesn't 
        // have access. (3) The semaphore exists and the user has
        // access.
        //
        if (doesNotExist)
        {
            // The semaphore does not exist, so create it.
            //
            // The value of this variable is set by the semaphore
            // constructor. It is true if the named system semaphore was
            // created, and false if the named semaphore already existed.
            //
            bool semaphoreWasCreated;

            // Create an access control list (ACL) that denies the
            // current user the right to enter or release the 
            // semaphore, but allows the right to read and change
            // security information for the semaphore.
            //
            string user = Environment.UserDomainName + "\\" 
                + Environment.UserName;
            SemaphoreSecurity semSec = new SemaphoreSecurity();

            SemaphoreAccessRule rule = new SemaphoreAccessRule(
                user, 
                SemaphoreRights.Synchronize | SemaphoreRights.Modify, 
                AccessControlType.Deny);
            semSec.AddAccessRule(rule);

            rule = new SemaphoreAccessRule(
                user, 
                SemaphoreRights.ReadPermissions | SemaphoreRights.ChangePermissions,
                AccessControlType.Allow);
            semSec.AddAccessRule(rule);

            // Create a Semaphore object that represents the system
            // semaphore named by the constant 'semaphoreName', with
            // maximum count three, initial count three, and the
            // specified security access. The Boolean value that 
            // indicates creation of the underlying system object is
            // placed in semaphoreWasCreated.
            //
            sem = new Semaphore(3, 3, semaphoreName, 
                out semaphoreWasCreated, semSec);

            // If the named system semaphore was created, it can be
            // used by the current instance of this program, even 
            // though the current user is denied access. The current
            // program enters the semaphore. Otherwise, exit the
            // program.
            // 
            if (semaphoreWasCreated)
            {
                Console.WriteLine("Created the semaphore.");
            }
            else
            {
                Console.WriteLine("Unable to create the semaphore.");
                return;
            }
        }
        else if (unauthorized)
        {
            // Open the semaphore to read and change the access
            // control security. The access control security defined
            // above allows the current user to do this.
            //
            try
            {
                sem = Semaphore.OpenExisting(
                    semaphoreName, 
                    SemaphoreRights.ReadPermissions 
                        | SemaphoreRights.ChangePermissions);

                // Get the current ACL. This requires 
                // SemaphoreRights.ReadPermissions.
                SemaphoreSecurity semSec = sem.GetAccessControl();
                
                string user = Environment.UserDomainName + "\\" 
                    + Environment.UserName;

                // First, the rule that denied the current user 
                // the right to enter and release the semaphore must
                // be removed.
                SemaphoreAccessRule rule = new SemaphoreAccessRule(
                    user, 
                    SemaphoreRights.Synchronize | SemaphoreRights.Modify, 
                    AccessControlType.Deny);
                semSec.RemoveAccessRule(rule);

                // Now grant the user the correct rights.
                // 
                rule = new SemaphoreAccessRule(user, 
                     SemaphoreRights.Synchronize | SemaphoreRights.Modify, 
                     AccessControlType.Allow);
                semSec.AddAccessRule(rule);

                // Update the ACL. This requires
                // SemaphoreRights.ChangePermissions.
                sem.SetAccessControl(semSec);

                Console.WriteLine("Updated semaphore security.");

                // Open the semaphore with (SemaphoreRights.Synchronize 
                // | SemaphoreRights.Modify), the rights required to
                // enter and release the semaphore.
                //
                sem = Semaphore.OpenExisting(semaphoreName);
            }
            catch(UnauthorizedAccessException ex)
            {
                Console.WriteLine("Unable to change permissions: {0}", ex.Message);
                return;
            }
        }

        // Enter the semaphore, and hold it until the program
        // exits.
        //
        try
        {
            sem.WaitOne();
            Console.WriteLine("Entered the semaphore.");
            Console.WriteLine("Press the Enter key to exit.");
            Console.ReadLine();
            sem.Release();
        }
        catch(UnauthorizedAccessException ex)
        {
            Console.WriteLine("Unauthorized access: {0}", ex.Message);
        }
    }
}

Commenti

Il GetAccessControl metodo usa la seguente combinazione di flag (combinati usando l'operazione OR bit per bit) per cercare le autorizzazioni: AccessControlSections.Access, AccessControlSections.Ownere AccessControlSections.Group.

L'utente deve disporre SemaphoreRights.ReadPermissions dei diritti per chiamare questo metodo e il semaforo deve essere stato aperto con SemaphoreRights.ReadPermissions diritti.

In un semaforo locale, la sicurezza del controllo di accesso è irrilevante. Se l'oggetto Semaphore non rappresenta un semaforo di sistema denominato, questo metodo restituisce un SemaphoreSecurity oggetto che concede tutti i diritti a qualsiasi utente.

Si applica a

Prodotto Versioni
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Vedi anche