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

注解

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)

适用于

另请参阅