Share via


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.SetAccessControl中找到Semaphore.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.
        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)

適用於

另請參閱