共用方式為


安全性描述元管理

並非所有工作都可藉由修改物件的屬性來執行。有時候必須修改物件的安全性描述元。例如,依照預設,一般使用者可以讀取大多數的屬性,但無法修改它們。您可能要讓使用者可讀取和寫入其電話號碼。這可藉由新增存取控制項目 (ACE) 至使用者物件的判別存取控制清單 (DACL) 來完成。此 ACE 為允許 ACE,允許寫入 telephoneNumber 屬性。如需有關 telephoneNumber 屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<telephoneNumber>或<Telephone-Number>主題。

在 1.0 版與 1.1 版的 .NET Framework 中,預設不支援修改 Active Directory 物件的安全性描述元。Active Directory 物件的安全性描述元可藉由叫用 ADSI 並使用 IADsSecurityDescriptor 及相關介面來處理。如需詳細資訊,請參閱 叫用 ADSI。如需有關 IADsSecurityDescriptor 介面的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsSecurityDescriptor>主題。

在 .NET Framework 2.0 中,System.DirectoryServices 命名空間包含幾個類別,允許處理安全性描述元而不叫用 ADSI。搭配 Active Directory 物件安全性描述元使用的主要類別為 ActiveDirectorySecurity。您可以取得 ActiveDirectorySecurity 物件,它代表具有 ObjectSecurity 屬性的 Active Directory 物件的安全性描述元。

ActiveDirectorySecurity 類別具有允許讀取和修改物件的判別存取控制清單與次要控制清單的方法與屬性。

判別存取控制清單上的存取控制項目稱為存取規則。代表存取規則的主要類別為 ActiveDirectoryAccessRule 類別。還有幾個衍生自 ActiveDirectoryAccessRule 類別的類別,可用來代表存取規則的特定類型。下表列出這些特定類別及其用法。

類別 用法

CreateChildAccessRule

代表用來允許或拒絕 Active Directory 物件建立子物件之權限的存取規則。

DeleteChildAccessRule

代表用來允許或拒絕 Active Directory 物件刪除子物件之權限的存取規則。

DeleteTreeAccessRule

代表用來允許或拒絕 Active Directory 物件刪除所有子物件之權限的存取規則。

ExtendedRightAccessRule

代表用來允許或拒絕 Active Directory 物件之延伸權限的存取規則。

ListChildrenAccessRule

代表用來允許或拒絕 Active Directory 物件列出子物件之權限的存取規則。

PropertyAccessRule

代表用來允許或拒絕存取 Active Directory 屬性的存取規則。

PropertySetAccessRule

代表用來允許或拒絕存取 Active Directory 屬性集的存取規則。

系統存取控制清單上的存取控制項目稱為稽核規則,以 ActiveDirectoryAuditRule 類別表示。

在許多情況下,新增存取規則至每一個別物件以允許或拒絕存取某項目並不切實際。在這些情況下,最好修改容器的安全性描述元並使存取規則可被繼承。如此也可確保在建立新物件時,它們將自動取得適當的權限。在上面提供的 telephoneNumber 範例中,您可能要允許所有使用者擁有其電話號碼的寫入權限。最簡單的方式是新增可繼承的存取規則至容器,例如組織單元,然後將所有使用者置於該組織單元之下。下列程式碼範例示範如何執行此工作。

範例

下列 Visual Basic .NET 範例示範如何新增存取規則至容器,以將 telephoneNumber 屬性的寫入權限提供給所有容器的使用者物件子項。如需有關 telephoneNumber 屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<telephoneNumber>或<Telephone-Number>主題。

Sub SetWritePhonePermission(ByVal container As DirectoryEntry)
    Try
        ' Get the ActiveDirectorySecurity for the container.
        Dim containerSecurity As ActiveDirectorySecurity
        containerSecurity = container.ObjectSecurity

        ' Create a SecurityIdentifier object for "self".
        Dim selfSid As New SecurityIdentifier(WellKnownSidType.SelfSid, _
            Nothing)

        ' Get the schema for the currently logged on user.
        Dim schema As ActiveDirectorySchema
        schema = ActiveDirectorySchema.GetCurrentSchema()

        ' Get the telephoneNumber schema property object.
        Dim phoneProperty As ActiveDirectorySchemaProperty
        phoneProperty = schema.FindProperty("telephoneNumber")

        ' Get the user schema class object.
        Dim userClass As ActiveDirectorySchemaClass
        userClass = schema.FindClass("user")

        ' Create a property access rule to allow a user to write to their own telephoneNumber property.
        Dim allowWritePhoneRule As New PropertyAccessRule(selfSid, _
            AccessControlType.Allow, _
            PropertyAccess.Write, _
            phoneProperty.SchemaGuid, _
            ActiveDirectorySecurityInheritance.Descendents, _
            userClass.SchemaGuid)

        ' Add the access rule to the DACL.
        container.ObjectSecurity.AddAccessRule(allowWritePhoneRule)

        ' Commit the changes.
        container.CommitChanges()

    Catch notFoundEx As ActiveDirectoryObjectNotFoundException
        ' The schema class or property could not be found.
    End Try

End Sub 'SetWritePhonePermission

下列 C# 範例示範如何新增存取規則至容器,將 telephoneNumber 屬性的寫入權限提供給所有容器的使用者物件子項。如需有關 telephoneNumber 屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<telephoneNumber>或<Telephone-Number>主題。

static void SetWritePhonePermission(DirectoryEntry container)
{
    try
    {
        // Get the ActiveDirectorySecurity for the container.
        ActiveDirectorySecurity containerSecurity = container.ObjectSecurity;

        // Create a SecurityIdentifier object for "self".
        SecurityIdentifier selfSid =
            new SecurityIdentifier(WellKnownSidType.SelfSid, null);

        // Get the schema for the currently logged on user.
        ActiveDirectorySchema schema = ActiveDirectorySchema.GetCurrentSchema();

        // Get the telephoneNumber schema property object.
        ActiveDirectorySchemaProperty phoneProperty = schema.FindProperty("telephoneNumber");

        // Get the user schema class object.
        ActiveDirectorySchemaClass userClass = schema.FindClass("user");

        // Create a property access rule to allow a user to write to their own telephoneNumber property.
        PropertyAccessRule allowWritePhoneRule =
            new PropertyAccessRule(
                selfSid,
                AccessControlType.Allow,
                PropertyAccess.Write,
                phoneProperty.SchemaGuid,
                ActiveDirectorySecurityInheritance.Descendents,
                userClass.SchemaGuid);

        // Add the access rule to the DACL.
        container.ObjectSecurity.AddAccessRule(allowWritePhoneRule);

        // Commit the changes.
        container.CommitChanges();
    }
    catch (ActiveDirectoryObjectNotFoundException)
    {
        // The schema class or property could not be found.
    }
}

請參閱

參考

System.DirectoryServices
ActiveDirectorySecurity
ActiveDirectoryAccessRule
ActiveDirectoryAuditRule

概念

系統管理工作

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.