MutexAccessRule 類別

定義

表示允許或拒絕使用者或群組的一組存取權限。 此類別無法獲得繼承。

public ref class MutexAccessRule sealed : System::Security::AccessControl::AccessRule
public sealed class MutexAccessRule : System.Security.AccessControl.AccessRule
[System.Security.SecurityCritical]
public sealed class MutexAccessRule : System.Security.AccessControl.AccessRule
type MutexAccessRule = class
    inherit AccessRule
[<System.Security.SecurityCritical>]
type MutexAccessRule = class
    inherit AccessRule
Public NotInheritable Class MutexAccessRule
Inherits AccessRule
繼承
屬性

範例

下列程式碼範例示範物件的建立和使用 MutexAccessRule 。 此範例會 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

備註

類別 MutexAccessRule 是一組類別,.NET Framework提供來管理具名系統 Mutex 上的 Windows 存取控制安全性。 如需這些類別的概觀及其與基礎 Windows 存取控制結構的關聯性,請參閱 MutexSecurity

注意

Windows 存取控制安全性僅適用于具名系統 Mutex。 Mutex如果 物件代表本機 Mutex,存取控制就不相關。

若要取得目前套用至具名 Mutex 的規則清單,請使用 Mutex.GetAccessControl 方法來取得 MutexSecurity 物件,然後使用其 GetAccessRules 方法來取得 物件的集合 MutexAccessRule

MutexAccessRule 物件不會將一對一對應至基礎任意存取控制清單中的存取控制專案, (DACL) 。 當您取得 Mutex 的所有存取規則集時,此集合包含目前表示所有存取控制專案所需的最小規則數目。

注意

當您套用和移除規則時,基礎存取控制專案會變更。 如果可能的話,規則中的資訊會合並,以維護最少的存取控制專案數目。 因此,當您讀取目前的規則清單時,它看起來可能與您所新增的所有規則清單完全相同。

使用 MutexAccessRule 物件來指定允許或拒絕使用者或群組的存取權限。 MutexAccessRule物件一律代表允許的存取或拒絕存取,絕不會同時代表這兩者。

若要將規則套用至具名系統 Mutex,請使用 Mutex.GetAccessControl 方法來取得 MutexSecurity 物件。 MutexSecurity使用方法新增規則來修改物件,然後使用 Mutex.SetAccessControl 方法重新附加安全性物件。

重要

對物件所做的 MutexSecurity 變更不會影響具名 Mutex 的存取層級,直到您呼叫 Mutex.SetAccessControl 方法,將改變的安全性物件指派給具名 Mutex 為止。

MutexAccessRule 物件是不可變的。 Mutex 的安全性是使用 類別的 MutexSecurity 方法來修改,以新增或移除規則;如您所做,會修改基礎存取控制專案。

建構函式

MutexAccessRule(IdentityReference, MutexRights, AccessControlType)

初始化 MutexAccessRule 類別的新執行個體,指定套用此規則的使用者或群組、存取權限,以及允許還是拒絕指定的存取權限。

MutexAccessRule(String, MutexRights, AccessControlType)

初始化 MutexAccessRule 類別的新執行個體,指定套用此規則的使用者或群組名稱、存取權限,以及允許還是拒絕指定的存取權限。

屬性

AccessControlType

取得與這個 AccessControlType 物件相關聯的 AccessRule 值。

(繼承來源 AccessRule)
AccessMask

取得此規則的存取遮罩。

(繼承來源 AuthorizationRule)
IdentityReference

取得要套用此規則的 IdentityReference

(繼承來源 AuthorizationRule)
InheritanceFlags

取得旗標值,這個值會決定子物件繼承這項規則的方式。

(繼承來源 AuthorizationRule)
IsInherited

取得值,表示這個規則是否會明確地設定或繼承自父容器物件。

(繼承來源 AuthorizationRule)
MutexRights

取得存取規則所允許或拒絕的權限。

PropagationFlags

取得傳播旗標的值,該值判斷這個規則的繼承如何傳播到子物件。 只有當 InheritanceFlags 列舉的值不是 None,這個屬性才是重要的。

(繼承來源 AuthorizationRule)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱