Esempio n. 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.
        EventWaitHandleSecurity mSec = new EventWaitHandleSecurity();

        // Add a rule that grants the current user the
        // right to wait on or signal the event.
        EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user,
                                                                       EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify,
                                                                       AccessControlType.Allow);

        mSec.AddAccessRule(rule);

        // Add a rule that denies the current user the
        // right to change permissions on the event.
        rule = new EventWaitHandleAccessRule(user,
                                             EventWaitHandleRights.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 event. This rule
        // is merged with the existing Allow rule.
        rule = new EventWaitHandleAccessRule(user,
                                             EventWaitHandleRights.ReadPermissions,
                                             AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        ShowSecurity(mSec);

        // Create a rule that allows the current user to
        // take ownership of the event, and use that rule
        // to remove the existing Allow rule from the
        // EventWaitHandleSecurity 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 EventWaitHandleAccessRule(user,
                                             EventWaitHandleRights.TakeOwnership,
                                             AccessControlType.Allow);
        mSec.RemoveAccessRuleAll(rule);

        ShowSecurity(mSec);
    }