MutexAccessRule 类

定义

表示一组允许或拒绝用户或组进行访问的权限。 此类不能被继承。

public ref class MutexAccessRule sealed : System::Security::AccessControl::AccessRule
public sealed class MutexAccessRule : System.Security.AccessControl.AccessRule
[System.Security.SecurityCritical]
public sealed class MutexAccessRule : System.Security.AccessControl.AccessRule
type MutexAccessRule = class
    inherit AccessRule
[<System.Security.SecurityCritical>]
type MutexAccessRule = class
    inherit AccessRule
Public NotInheritable Class MutexAccessRule
Inherits AccessRule
继承
属性

示例

下面的代码示例演示如何创建和使用 MutexAccessRule 对象。 该示例创建 一个 MutexSecurity 对象,添加允许和拒绝当前用户的各种权限的规则,并显示生成的规则对。 然后,该示例允许当前用户的新权限并显示结果,显示新权限与现有 Allow 规则合并。

注意

此示例不将安全对象附加到 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.
        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
 */
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.
        Dim rule As New MutexAccessRule(user, _
            MutexRights.Synchronize _
            Or 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)

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

注解

MutexAccessRule是.NET Framework提供的一组类之一,用于管理命名系统互斥体上的 Windows 访问控制安全性。 有关这些类及其与基础 Windows 访问控制结构的关系的概述,请参阅 MutexSecurity

注意

Windows 访问控制安全性仅对命名系统互斥体有意义。 Mutex如果 对象表示本地互斥体,则访问控制无关紧要。

若要获取当前应用于命名互斥体的规则列表,请使用 Mutex.GetAccessControl 方法获取 MutexSecurity 对象,然后使用其 GetAccessRules 方法获取 对象的集合 MutexAccessRule

MutexAccessRule 对象不会使用基础自由访问控制列表中的访问控制条目进行一对一映射, (DACL) 。 获取互斥锁的所有访问规则集时,该集包含表示所有访问控制项所需的当前最小规则数。

注意

应用和删除规则时,基础访问控制条目会更改。 如果可能,将合并规则中的信息,以保持最少数量的访问控制条目。 因此,当你阅读当前规则列表时,它可能看起来与你添加的所有规则的列表不完全相同。

使用 MutexAccessRule 对象指定对用户或组允许或拒绝的访问权限。 MutexAccessRule对象始终表示允许的访问或拒绝的访问,从不同时表示两者。

若要将规则应用于命名系统互斥体,请使用 Mutex.GetAccessControl 方法获取 MutexSecurity 对象。 MutexSecurity通过使用对象的方法添加规则来修改对象,然后使用 Mutex.SetAccessControl 方法重新附加安全对象。

重要

在调用 Mutex.SetAccessControl 方法将已更改的安全对象分配给命名互斥体之前,对对象所做的更改MutexSecurity不会影响命名互斥体的访问级别。

MutexAccessRule 对象是不可变的。 使用 类的方法 MutexSecurity 修改互斥体的安全性以添加或删除规则;执行此操作时,会修改基础访问控制条目。

构造函数

MutexAccessRule(IdentityReference, MutexRights, AccessControlType)

初始化 MutexAccessRule 类的新实例,指定此规则应用到的用户或组、访问权限以及是否允许或拒绝指定的访问权限。

MutexAccessRule(String, MutexRights, AccessControlType)

初始化 MutexAccessRule 类的新实例,指定应用此规则的用户或组的名称、访问权限以及是否允许或拒绝指定的访问权限。

属性

AccessControlType

获取与此 AccessControlType 对象关联的 AccessRule 对象。

(继承自 AccessRule)
AccessMask

获取此规则的访问掩码。

(继承自 AuthorizationRule)
IdentityReference

获取对其应用此规则的 IdentityReference

(继承自 AuthorizationRule)
InheritanceFlags

获取用于确定子对象如何继承此规则的标志的值。

(继承自 AuthorizationRule)
IsInherited

获取一个值,该值指示此规则是否为显式设置或继承自父级容器对象。

(继承自 AuthorizationRule)
MutexRights

获取访问规则允许或拒绝的权限。

PropagationFlags

获取传播标志的值,该值确定如何将此规则的继承传播到子对象。 仅当 InheritanceFlags 枚举的值不为 None 时,此属性才有意义。

(继承自 AuthorizationRule)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅