MutexSecurity 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示命名的 mutex 的 Windows 访问控制安全性。 此类不能被继承。
public ref class MutexSecurity sealed : System::Security::AccessControl::NativeObjectSecuritypublic sealed class MutexSecurity : System.Security.AccessControl.NativeObjectSecurity[System.Security.SecurityCritical]
public sealed class MutexSecurity : System.Security.AccessControl.NativeObjectSecuritytype MutexSecurity = class
    inherit NativeObjectSecurity[<System.Security.SecurityCritical>]
type MutexSecurity = class
    inherit NativeObjectSecurityPublic NotInheritable Class MutexSecurity
Inherits NativeObjectSecurity- 继承
- 属性
示例
下面的代码示例演示了规则和Deny规则之间的Allow分离,并演示了兼容规则中权限的组合。 该示例创建 一个 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
注解
对象 MutexSecurity 指定命名系统互斥体的访问权限,还指定如何审核访问尝试。 互斥体的访问权限表示为规则,每个访问规则由 对象 MutexAccessRule 表示。 每个审核规则都由 一个 MutexAuditRule 对象表示。
这反映了基础 Windows 安全系统,其中每个安全对象最多有一个可自由访问控制列表, (DACL) 控制对受保护对象的访问,最多有一个系统访问控制列表 (SACL) ,用于指定审核哪些访问尝试。 DACL 和 SACL 是访问控制条目的有序列表, (ACE) 指定用户和组的访问和审核。 或 MutexAccessRuleMutexAuditRule 对象可能表示多个 ACE。
注意
对象 Mutex 可以表示本地互斥体或命名系统互斥体。 Windows 访问控制安全性仅对命名系统互斥体有意义。
MutexSecurity、 MutexAccessRule和 MutexAuditRule 类隐藏 ACL 和 ACE 的实现详细信息。 它们允许忽略 17 种不同的 ACE 类型,以及正确维护访问权限继承和传播的复杂性。 这些对象还设计为防止以下常见访问控制错误:
- 创建具有 null DACL 的安全描述符。 对 DACL 的空引用允许任何用户向对象添加访问规则,从而可能引发拒绝服务攻击。 新 MutexSecurity 对象始终以空 DACL 开头,这会拒绝所有用户的所有访问。 
- 违反 ACE 的规范顺序。 如果 DACL 中的 ACE 列表未按规范顺序保留,则用户可能会无意中获得对受保护对象的访问权限。 例如,拒绝的访问权限必须始终出现在允许的访问权限之前。 MutexSecurity 对象在内部保持正确的顺序。 
- 操作安全描述符标志,这些标志应仅受资源管理器控制。 
- 创建无效的 ACE 标志组合。 
- 操作继承的 ACE。 继承和传播由资源管理器处理,以响应对访问和审核规则所做的更改。 
- 将无意义的 ACE 插入 ACL。 
.NET 安全对象不支持的唯一功能是大多数应用程序开发人员应避免的危险活动,如下所示:
- 通常由资源管理器执行的低级别任务。 
- 以不维护规范顺序的方式添加或删除访问控制条目。 
若要修改命名互斥体的 Windows 访问控制安全性,请使用 Mutex.GetAccessControl 方法获取 对象 MutexSecurity 。 通过添加和删除规则修改安全对象,然后使用 Mutex.SetAccessControl 方法重新附加它。
重要
在调用 Mutex.SetAccessControl 方法将MutexSecurity更改的安全对象分配给命名互斥体之前,对对象所做的更改不会影响命名互斥对象的访问级别。
若要将访问控制安全性从一个互斥体复制到另一个互斥体,请使用 Mutex.GetAccessControl 方法获取表示第一个 MutexSecurity 互斥体的访问和审核规则的对象,然后使用 Mutex.SetAccessControl 方法或接受对象的构造函数将这些规则分配给第二个 MutexSecurity 互斥体。
投资安全描述符定义语言 (SDDL) 的用户可以使用 SetSecurityDescriptorSddlForm 方法为命名互斥体设置访问规则,并使用 GetSecurityDescriptorSddlForm 方法获取表示 SDDL 格式的访问规则的字符串。 对于新开发,不建议这样做。
构造函数
| MutexSecurity() | 使用默认值初始化 MutexSecurity 类的新实例。 | 
| MutexSecurity(String, AccessControlSections) | 使用来自具有指定名称的系统 mutex 的访问控制安全性规则的指定部分初始化 MutexSecurity 类的新实例。 |