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