Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

MutexAccessRule Class

Note: This class is new in the .NET Framework version 2.0.

Represents a set of access rights allowed or denied for a user or group. This class cannot be inherited.

Namespace: System.Security.AccessControl
Assembly: mscorlib (in mscorlib.dll)

public sealed class MutexAccessRule : AccessRule
public final class MutexAccessRule extends AccessRule
public final class MutexAccessRule extends AccessRule

The MutexAccessRule class is one of a set of classes that the .NET Framework provides for managing Windows access control security on named system mutexes. For an overview of these classes, and their relationship to the underlying Windows access control structures, see MutexSecurity.

NoteNote

Windows access control security is meaningful only for named system mutexes. If a Mutex object represents a local mutex, access control is irrelevant.

To get a list of the rules currently applied to a named mutex, use the Mutex.GetAccessControl method to get a MutexSecurity object, and then use its GetAccessRules method to obtain a collection of MutexAccessRule objects.

MutexAccessRule objects do not map one-to-one with access control entries in the underlying discretionary access control list (DACL). When you get the set of all access rules for a mutex, the set contains the minimum number of rules currently required to express all the access control entries.

NoteNote

The underlying access control entries change as you apply and remove rules. The information in rules is merged if possible, to maintain the smallest number of access control entries. Thus, when you read the current list of rules, it might not look exactly like the list of all the rules you have added.

Use MutexAccessRule objects to specify access rights to allow or deny to a user or group. A MutexAccessRule object always represents either allowed access or denied access, never both.

To apply a rule to a named system mutex, use the Mutex.GetAccessControl method to get the MutexSecurity object. Modify the MutexSecurity object by using its methods to add the rule, and then use the Mutex.SetAccessControl method to reattach the security object.

NoteImportant:

Changes you make to a MutexSecurity object do not affect the access levels of the named mutex until you call the Mutex.SetAccessControl method to assign the altered security object to the named mutex.

MutexAccessRule objects are immutable. Security for a mutex is modified using the methods of the MutexSecurity class to add or remove rules; as you do this, the underlying access control entries are modified.

The following code example demonstrates the creation and use of MutexAccessRule objects. The example creates a MutexSecurity object, adds rules that allow and deny various rights for the current user, and displays the resulting pair of rules. The example then allows new rights for the current user and displays the result, showing that the new rights are merged with the existing Allow rule.

NoteNote

This example does not attach the security object to a Mutex object. Examples that attach security objects can be found in Mutex.GetAccessControl and Mutex.SetAccessControl.

using System;
using System.Threading;
using System.Security.AccessControl;
using System.Security.Principal;

public class Example
{
    public static void Main()
    {
        // Create a string representing the current user.
        string user = Environment.UserDomainName + "\\" + 
            Environment.UserName;

        // Create a security object that grants no access.
        MutexSecurity mSec = new MutexSecurity();

        // Add a rule that grants the current user the 
        // right to enter or release the mutex.
        MutexAccessRule rule = new MutexAccessRule(user, 
            MutexRights.Synchronize | MutexRights.Modify, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        // Add a rule that denies the current user the 
        // right to change permissions on the mutex.
        rule = new MutexAccessRule(user, 
            MutexRights.ChangePermissions, 
            AccessControlType.Deny);
        mSec.AddAccessRule(rule);

        // Display the rules in the security object.
        ShowSecurity(mSec);

        // Add a rule that allows the current user the 
        // right to read permissions on the mutex. This rule
        // is merged with the existing Allow rule.
        rule = new MutexAccessRule(user, 
            MutexRights.ReadPermissions, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        ShowSecurity(mSec);
    }

    private static void ShowSecurity(MutexSecurity security)
    {
        Console.WriteLine("\r\nCurrent access rules:\r\n");

        foreach(MutexAccessRule ar in 
            security.GetAccessRules(true, true, typeof(NTAccount)))
        {
            Console.WriteLine("        User: {0}", ar.IdentityReference);
            Console.WriteLine("        Type: {0}", ar.AccessControlType);
            Console.WriteLine("      Rights: {0}", ar.MutexRights);
            Console.WriteLine();
        }
    }
}

/*This code example produces output similar to following:

Current access rules:

        User: TestDomain\TestUser
        Type: Deny
      Rights: ChangePermissions

        User: TestDomain\TestUser
        Type: Allow
      Rights: Modify, Synchronize


Current access rules:

        User: TestDomain\TestUser
        Type: Deny
      Rights: ChangePermissions

        User: TestDomain\TestUser
        Type: Allow
      Rights: Modify, ReadPermissions, Synchronize
 */

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.