SemaphoreAccessRule 类

定义

表示用户或组允许或拒绝的一组访问权限。 无法继承此类。

public ref class SemaphoreAccessRule sealed : System::Security::AccessControl::AccessRule
public sealed class SemaphoreAccessRule : System.Security.AccessControl.AccessRule
[System.Security.SecurityCritical]
public sealed class SemaphoreAccessRule : System.Security.AccessControl.AccessRule
[System.Runtime.InteropServices.ComVisible(false)]
public sealed class SemaphoreAccessRule : System.Security.AccessControl.AccessRule
type SemaphoreAccessRule = class
    inherit AccessRule
[<System.Security.SecurityCritical>]
type SemaphoreAccessRule = class
    inherit AccessRule
[<System.Runtime.InteropServices.ComVisible(false)>]
type SemaphoreAccessRule = class
    inherit AccessRule
Public NotInheritable Class SemaphoreAccessRule
Inherits AccessRule
继承
SemaphoreAccessRule
属性

示例

下面的代码示例演示规则和Deny规则之间的Allow分离,并显示了兼容规则中权限的组合。 该示例创建一个 SemaphoreSecurity 对象,添加允许和拒绝当前用户的各种权限的规则,并显示生成的规则对。 然后,该示例允许当前用户的新权限并显示结果,显示新权限与现有 Allow 规则合并。

注释

此示例不将安全对象附加到对象 Semaphore 。 可以在其中找到 Semaphore.GetAccessControl 附加安全对象的示例 Semaphore.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.
        SemaphoreSecurity mSec = new SemaphoreSecurity();

        // Add a rule that grants the current user the 
        // right to enter or release the semaphore.
        SemaphoreAccessRule rule = new SemaphoreAccessRule(user, 
            SemaphoreRights.Synchronize | SemaphoreRights.Modify, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        // Add a rule that denies the current user the 
        // right to change permissions on the semaphore.
        rule = new SemaphoreAccessRule(user, 
            SemaphoreRights.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 semaphore. This rule
        // is merged with the existing Allow rule.
        rule = new SemaphoreAccessRule(user, 
            SemaphoreRights.ReadPermissions, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        ShowSecurity(mSec);
    }

    private static void ShowSecurity(SemaphoreSecurity security)
    {
        Console.WriteLine("\r\nCurrent access rules:\r\n");

        foreach(SemaphoreAccessRule 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.SemaphoreRights);
            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 SemaphoreSecurity()

        ' Add a rule that grants the current user the 
        ' right to enter or release the semaphore.
        Dim rule As New SemaphoreAccessRule(user, _
            SemaphoreRights.Synchronize _
            Or SemaphoreRights.Modify, _
            AccessControlType.Allow)
        mSec.AddAccessRule(rule)

        ' Add a rule that denies the current user the 
        ' right to change permissions on the semaphore.
        rule = New SemaphoreAccessRule(user, _
            SemaphoreRights.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 semaphore. This 
        ' rule is merged with the existing Allow rule.
        rule = New SemaphoreAccessRule(user, _
            SemaphoreRights.ReadPermissions, _
            AccessControlType.Allow)
        mSec.AddAccessRule(rule)

        ShowSecurity(mSec)

    End Sub 

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

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

            Console.WriteLine("        User: {0}", ar.IdentityReference)
            Console.WriteLine("        Type: {0}", ar.AccessControlType)
            Console.WriteLine("      Rights: {0}", ar.SemaphoreRights)
            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 上受支持。

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

注释

Windows 访问控制安全性仅对命名系统信号量有意义。 Semaphore如果对象表示本地信号灯,则访问控制无关。

若要获取当前应用于命名信号量的规则列表,请使用 Semaphore.GetAccessControl 该方法获取对象 SemaphoreSecurity ,然后使用其 GetAccessRules 方法获取对象的集合 SemaphoreAccessRule

SemaphoreAccessRule 对象不映射一对一的访问控制条目,这些条目位于基础自由访问控制列表 (DACL) 中。 获取信号灯的所有访问规则集时,该集包含当前表示所有访问控制条目所需的最小规则数。

注释

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

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

若要将规则应用于命名系统信号灯,请使用 Semaphore.GetAccessControl 该方法获取 SemaphoreSecurity 对象。 SemaphoreSecurity通过使用对象的方法添加规则来修改对象,然后使用Semaphore.SetAccessControl该方法重新附加安全对象。

重要

SemaphoreSecurity 对象的更改不会影响命名信号灯的访问级别,直到调用 Semaphore.SetAccessControl 方法以将更改的安全对象分配给命名信号量。

SemaphoreAccessRule 对象是不可变的。 使用类的方法 SemaphoreSecurity 修改信号灯的安全性以添加或删除规则;执行此作时,将修改基础访问控制条目。

构造函数

名称 说明
SemaphoreAccessRule(IdentityReference, SemaphoreRights, AccessControlType)

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

SemaphoreAccessRule(String, SemaphoreRights, AccessControlType)

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

属性

名称 说明
AccessControlType

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

(继承自 AccessRule)
AccessMask

获取此规则的访问掩码。

(继承自 AuthorizationRule)
IdentityReference

IdentityReference获取此规则适用的对象。

(继承自 AuthorizationRule)
InheritanceFlags

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

(继承自 AuthorizationRule)
IsInherited

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

(继承自 AuthorizationRule)
PropagationFlags

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

(继承自 AuthorizationRule)
SemaphoreRights

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

方法

名称 说明
Equals(Object)

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

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

Type获取当前实例。

(继承自 Object)
MemberwiseClone()

创建当前 Object副本的浅表副本。

(继承自 Object)
ToString()

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

(继承自 Object)

适用于

另请参阅