MutexSecurity.RemoveAccessRule(MutexAccessRule) メソッド

定義

指定した規則と同じユーザーと AccessControlType (許可または拒否)、および互換性のある継承フラグと反映フラグが指定されたアクセス制御規則が検索されます。このような規則が見つかると、指定したアクセス規則に含まれる権限がその規則から削除されます。

public:
 bool RemoveAccessRule(System::Security::AccessControl::MutexAccessRule ^ rule);
public bool RemoveAccessRule (System.Security.AccessControl.MutexAccessRule rule);
override this.RemoveAccessRule : System.Security.AccessControl.MutexAccessRule -> bool
Public Function RemoveAccessRule (rule As MutexAccessRule) As Boolean

パラメーター

rule
MutexAccessRule

検索対象のユーザーと MutexAccessRule、および一致する規則が見つかった場合にその規則と互換性のある継承フラグと反映フラグのセットを指定する AccessControlType。 互換性のある規則が見つかった場合にその規則から削除する権限を指定します。

戻り値

互換性のある規則が見つかった場合は true。それ以外の場合は false

例外

rulenullです。

次のコード例では、 メソッドを使用して、オブジェクト内の RemoveAccessRule ルールから権限を Allow 削除する方法を MutexSecurity 示します。 また、 の他の rule 権限が無視されていることも示しています。

この例では、 オブジェクトを MutexSecurity 作成し、現在のユーザーのさまざまな権限を許可および拒否する規則を追加します。 許可される権限には、、ReadPermissions、および がSynchronize含まれますModify。 次に、 と 権限を含むReadPermissionsTakeOwnership現在のユーザーの新しいルールを作成し、 メソッドでそのルールをRemoveAccessRule使用して オブジェクト内のMutexSecurityAllowルールから削除ReadPermissionsします。 の余分な TakeOwnership 権限 rule は無視されます。

Note

この例では、セキュリティ オブジェクトを オブジェクトに Mutex アタッチしません。 セキュリティ オブジェクトをアタッチする例は、 と Mutex.SetAccessControlにありますMutex.GetAccessControl

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 and read the
        // permissions on the mutex.
        MutexAccessRule rule = new MutexAccessRule(user, 
            MutexRights.Synchronize | MutexRights.Modify
                | MutexRights.ReadPermissions, 
            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);

        // Create a rule that grants the current user 
        // the right to read permissions on the mutex, and
        // take ownership of the mutex. Use this rule to 
        // remove the right to read permissions from the 
        // Allow rule for the current user. The inclusion 
        // of the right to take ownership has no effect.
        rule = new MutexAccessRule(user, 
            MutexRights.TakeOwnership | 
                MutexRights.ReadPermissions, 
            AccessControlType.Allow);
        mSec.RemoveAccessRule(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, ReadPermissions, Synchronize


Current access rules:

        User: TestDomain\TestUser
        Type: Deny
      Rights: ChangePermissions

        User: TestDomain\TestUser
        Type: Allow
      Rights: Modify, Synchronize
 */
Imports System.Threading
Imports System.Security.AccessControl
Imports System.Security.Principal

Public Class Example

    Public Shared Sub Main()

        ' Create a string representing the current user.
        Dim user As String = Environment.UserDomainName _ 
            & "\" & Environment.UserName

        ' Create a security object that grants no access.
        Dim mSec As New MutexSecurity()

        ' Add a rule that grants the current user the 
        ' right to enter or release the mutex, and to 
        ' read its permissions.
        Dim rule As New MutexAccessRule(user, _
            MutexRights.Synchronize _
            Or MutexRights.Modify _
            Or MutexRights.ReadPermissions, _
            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)

        ' Create a rule that grants the current user 
        ' the right to read permissions on the mutex, and
        ' take ownership of the mutex. Use this rule to 
        ' remove the right to read permissions from the 
        ' Allow rule for the current user. The inclusion 
        ' of the right to take ownership has no effect.
        rule = New MutexAccessRule(user, _
            MutexRights.TakeOwnership _
            Or MutexRights.ReadPermissions, _
            AccessControlType.Allow)
        mSec.RemoveAccessRule(rule)

        ShowSecurity(mSec)
        
    End Sub 

    Private Shared Sub ShowSecurity(ByVal security As MutexSecurity)
        Console.WriteLine(vbCrLf & "Current access rules:" & vbCrLf)

        For Each ar As MutexAccessRule In _
            security.GetAccessRules(True, True, GetType(NTAccount))

            Console.WriteLine("        User: {0}", ar.IdentityReference)
            Console.WriteLine("        Type: {0}", ar.AccessControlType)
            Console.WriteLine("      Rights: {0}", ar.MutexRights)
            Console.WriteLine()
        Next

    End Sub
End Class 

'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, ReadPermissions, Synchronize
'
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Deny
'      Rights: ChangePermissions
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: Modify, Synchronize

注釈

現在の MutexSecurity は、 と同じユーザーと同じ AccessControlTyperuleを持つルールを検索します。 このようなルールが見つからない場合、アクションは実行されません。メソッドは を返します false。 一致する規則が見つかった場合、継承フラグと互換性フラグは、 で rule指定されたフラグとの互換性をチェックします。 互換性のあるルールが見つからない場合、アクションは実行されません。メソッドは を返します false。 互換性のあるフラグを持つ規則が見つかった場合、 で rule 指定された権限は互換性のある規則から削除され、 メソッドは を返します true。 が互換性のあるルールに含まれていない権限を指定した場合 rule 、それらの権限に関するアクションは実行されません。 互換性のあるルールからすべての権限が削除された場合、ルール全体が現在 MutexSecurity のオブジェクトから削除されます。

重要

ミューテックス アクセス 規則の継承フラグと伝達フラグを指定できますが、 メソッドを使用 AccessRuleFactory して作成することはお勧めしません。 継承と伝達は名前付きミューテックスには意味がなく、アクセス規則のメンテナンスがより複雑になります。

適用対象