ServiceAuthorizationManager 類別

定義

提供服務作業的授權存取檢查。

public ref class ServiceAuthorizationManager
public class ServiceAuthorizationManager
type ServiceAuthorizationManager = class
Public Class ServiceAuthorizationManager
繼承
ServiceAuthorizationManager

範例

下列範例會示範名為 MyServiceAuthorizationManager 的類別,此類別繼承自 ServiceAuthorizationManager,而且會覆寫 CheckAccessCore 方法。

public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
  protected override bool CheckAccessCore(OperationContext operationContext)
  {
    // Extract the action URI from the OperationContext. Match this against the claims
    // in the AuthorizationContext.
    string action = operationContext.RequestContext.RequestMessage.Headers.Action;
  
    // Iterate through the various claim sets in the AuthorizationContext.
    foreach(ClaimSet cs in operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets)
    {
      // Examine only those claim sets issued by System.
      if (cs.Issuer == ClaimSet.System)
      {
        // Iterate through claims of type "http://www.contoso.com/claims/allowedoperation".
          foreach (Claim c in cs.FindClaims("http://www.contoso.com/claims/allowedoperation", Rights.PossessProperty))
        {
          // If the Claim resource matches the action URI then return true to allow access.
          if (action == c.Resource.ToString())
            return true;
        }
      }
    }
  
    // If this point is reached, return false to deny access.
    return false;
  }
}

Public Class MyServiceAuthorizationManager
    Inherits ServiceAuthorizationManager
    
    Protected Overrides Function CheckAccessCore(ByVal operationContext As OperationContext) As Boolean 
        ' Extract the action URI from the OperationContext. Match this against the claims.
        ' in the AuthorizationContext.
        Dim action As String = operationContext.RequestContext.RequestMessage.Headers.Action
        
        ' Iterate through the various claimsets in the AuthorizationContext.
        Dim cs As ClaimSet
        For Each cs In  operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets
            ' Examine only those claim sets issued by System.
            If cs.Issuer Is ClaimSet.System Then
                ' Iterate through claims of type "http://www.contoso.com/claims/allowedoperation".
                Dim c As Claim
                For Each c In  cs.FindClaims("http://www.contoso.com/claims/allowedoperation", _
                     Rights.PossessProperty)
                    ' If the Claim resource matches the action URI then return true to allow access.
                    If action = c.Resource.ToString() Then
                        Return True
                    End If
                Next c
            End If
        Next cs 
        ' If this point is reached, return false to deny access.
        Return False
    
    End Function 
End Class

備註

這個類別會負責評估所有原則 (定義允許使用者執行之作業的規則)、比較原則與用戶端所做的宣告、將結果 AuthorizationContext 設定為 ServiceSecurityContext,以及提供是否允許或拒絕呼叫者存取已指定服務作業的授權決策。

每次 CheckAccessCore 嘗試存取資源時,Windows Communication Foundation (WCF) 基礎結構都會呼叫 方法。 此方法會分別傳回 truefalse 以允許或拒絕存取。

ServiceAuthorizationManager是 WCF身分識別模型基礎結構的一部分。 「身分識別模型」可讓您建立自訂授權原則和自訂授權配置。 如需識別模型運作方式的詳細資訊,請參閱 使用身分識別模型管理宣告和授權

自訂授權

這個類別不會執行任何授權,而且會允許使用者存取所有服務作業。 若要提供更嚴格的授權,您必須建立會檢查自訂原則的自訂授權管理員。 若要這樣做,請繼承自這個類別並覆寫 CheckAccessCore 方法。 請透過 ServiceAuthorizationManager 屬性指定該衍生類別的執行個體。

CheckAccessCore 中,應用程式可以使用 OperationContext 物件來存取呼叫者身分識別 (ServiceSecurityContext)。

藉由取得會傳回 IncomingMessageHeaders 物件的 MessageHeaders 屬性,應用程式便可以存取服務 (To) 和作業 (Action)。

藉由取得會傳回 RequestContext 物件的 RequestContext 屬性,應用程式便可以存取整個要求訊息 (RequestMessage) 並據此執行授權決策。

如需範例,請參閱 如何:建立服務的自訂授權管理員

若要建立自訂授權原則,請實作 IAuthorizationPolicy 類別。 如需範例,請參閱 如何:建立自訂授權原則

若要建立自訂宣告,請使用 Claim 類別。 如需範例,請參閱 如何:建立自訂宣告。 若要比較自訂宣告,您必須比較宣告,如 如何:比較宣告所示。

如需詳細資訊,請參閱 自訂授權

您可以在用戶端應用程式組態檔中使用< serviceAuthorization >來設定自訂授權管理員的類型。

建構函式

ServiceAuthorizationManager()

初始化 ServiceAuthorizationManager 類別的新執行個體。

方法

CheckAccess(OperationContext)

檢查指定作業內容的授權。

CheckAccess(OperationContext, Message)

檢查在需要存取訊息時的作業內容授權。

CheckAccessCore(OperationContext)

檢查根據預設原則評估所指定作業內容的授權。

Equals(Object)

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

(繼承來源 Object)
GetAuthorizationPolicies(OperationContext)

取得參與原則評估的一組原則。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於

另請參閱