Beispiel #1
0
    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);

        // Create a rule that allows the current user to
        // change the owner of the mutex, and use that rule
        // to remove the existing allow access rule from
        // the MutexSecurity object, showing that the user
        // and access type must match, while the rights are
        // ignored.
        Console.WriteLine("Use RemoveAccessRuleAll to remove the Allow rule.");
        rule = new MutexAccessRule(user,
                                   MutexRights.TakeOwnership,
                                   AccessControlType.Allow);
        mSec.RemoveAccessRuleAll(rule);

        ShowSecurity(mSec);
    }