Прочитать на английском

Поделиться через


MutexSecurity.RemoveAccessRuleAll(MutexAccessRule) Метод

Определение

Осуществляет поиск всех правил управления доступом с тем же именем пользователя и значением свойства AccessControlType ("разрешить" или "запретить"), что и у указанного правила, и удаляет найденные правила.

public void RemoveAccessRuleAll(System.Security.AccessControl.MutexAccessRule rule);

Параметры

rule
MutexAccessRule

Объект MutexAccessRule, определяющий пользователя и значение AccessControlType для поиска. Все указанные в данном правиле права игнорируются.

Исключения

rule имеет значение null.

Примеры

В следующем примере кода показано, что RemoveAccessRuleAll метод удаляет все правила, соответствующие пользователю и AccessControlType, игнорируя права.

В примере создается MutexSecurity , добавляются правила, разрешающие и запрещающие различные права для текущего пользователя и затем объединяет дополнительные права в Allow правило. Затем в примере создается новое правило, текущий пользователь может стать владельцем и использует правила, чтобы удалить Allow правила из MutexSecurity объекта.

Примечание

В этом примере объект безопасности не присоединяет к объекту Mutex . Примеры присоединения объектов безопасности можно найти в Mutex.GetAccessControl и 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);

        // 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);
    }

    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

Use RemoveAccessRuleAll to remove the Allow rule.

Current access rules:

        User: TestDomain\TestUser
        Type: Deny
      Rights: ChangePermissions
 */

Комментарии

Текущий MutexSecurity объект ищется правила, имеющие тот же пользователь и имеет одинаковые AccessControlType значение как rule. Все права, указанные параметром rule , игнорируются при выполнении этого поиска. Если соответствующие правила не найдены, никаких действий не выполняется.

Применяется к

Продукт Версии
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.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
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10