Share via


ServiceAuthorizationManager.CheckAccess 메서드

정의

특정 작업 컨텍스트 및 선택적 메시지에 대한 권한 부여를 확인합니다.

오버로드

CheckAccess(OperationContext)

특정 작업 컨텍스트에 대한 권한 부여를 확인합니다.

CheckAccess(OperationContext, Message)

메시지에 액세스해야 할 때 특정 작업 컨텍스트에 대한 권한 부여를 확인합니다.

CheckAccess(OperationContext)

특정 작업 컨텍스트에 대한 권한 부여를 확인합니다.

public:
 virtual bool CheckAccess(System::ServiceModel::OperationContext ^ operationContext);
public virtual bool CheckAccess (System.ServiceModel.OperationContext operationContext);
abstract member CheckAccess : System.ServiceModel.OperationContext -> bool
override this.CheckAccess : System.ServiceModel.OperationContext -> bool
Public Overridable Function CheckAccess (operationContext As OperationContext) As Boolean

매개 변수

반환

Boolean

액세스 권한이 부여되면 true이고, 그렇지 않으면 false입니다. 기본값은 true입니다.

예제

다음 코드에서는 이 메서드를 재정의하여 사용자 지정 액세스 제어 요구 사항을 적용하는 방법을 보여 줍니다.

public class myServiceAuthorizationManager : ServiceAuthorizationManager
{
    // Override the CheckAccess method to enforce access control requirements.
    public override bool CheckAccess(OperationContext operationContext)
    {
        AuthorizationContext authContext =
        operationContext.ServiceSecurityContext.AuthorizationContext;
        if (authContext.ClaimSets == null) return false;
        if (authContext.ClaimSets.Count != 1) return false;
        ClaimSet myClaimSet = authContext.ClaimSets[0];
        if (!IssuedBySTS_B(myClaimSet)) return false;
        if (myClaimSet.Count != 1) return false;
        Claim myClaim = myClaimSet[0];
        if (myClaim.ClaimType ==
          "http://www.tmpuri.org:accessAuthorized")
        {
            string resource = myClaim.Resource as string;
            if (resource == null) return false;
            if (resource != "true") return false;
            return true;
        }
        else
        {
            return false;
        }
    }

    // This helper method checks whether SAML Token was issued by STS-B.
    // It compares the Thumbprint Claim of the Issuer against the
    // Certificate of STS-B.
    private bool IssuedBySTS_B(ClaimSet myClaimSet)
    {
        ClaimSet issuerClaimSet = myClaimSet.Issuer;
        if (issuerClaimSet == null) return false;
        if (issuerClaimSet.Count != 1) return false;
        Claim issuerClaim = issuerClaimSet[0];
        if (issuerClaim.ClaimType != ClaimTypes.Thumbprint)
            return false;
        if (issuerClaim.Resource == null) return false;
        byte[] claimThumbprint = (byte[])issuerClaim.Resource;
        // It is assumed that stsB_Certificate is a variable of type
        // X509Certificate2 that is initialized with the Certificate of
        // STS-B.
        X509Certificate2 stsB_Certificate = GetStsBCertificate();
        byte[] certThumbprint = stsB_Certificate.GetCertHash();
        if (claimThumbprint.Length != certThumbprint.Length)
            return false;
        for (int i = 0; i < claimThumbprint.Length; i++)
        {
            if (claimThumbprint[i] != certThumbprint[i]) return false;
        }
        return true;
    }
Public Class myServiceAuthorizationManager
    Inherits ServiceAuthorizationManager

    ' Override the CheckAccess method to enforce access control requirements.
    Public Overloads Overrides Function CheckAccess(ByVal operationContext As OperationContext) As Boolean
        Dim authContext = operationContext.ServiceSecurityContext.AuthorizationContext
        If authContext.ClaimSets Is Nothing Then
            Return False
        End If

        If authContext.ClaimSets.Count <> 1 Then
            Return False
        End If

        Dim myClaimSet = authContext.ClaimSets(0)
        If Not IssuedBySTS_B(myClaimSet) Then
            Return False
        End If
        If myClaimSet.Count <> 1 Then
            Return False
        End If
        Dim myClaim = myClaimSet(0)
        If myClaim.ClaimType = "http://www.tmpuri.org:accessAuthorized" Then
            Dim resource = TryCast(myClaim.Resource, String)
            If resource Is Nothing Then
                Return False
            End If
            If resource <> "true" Then
                Return False
            End If
            Return True
        Else
            Return False
        End If
    End Function

    ' This helper method checks whether SAML Token was issued by STS-B.     
    ' It compares the Thumbprint Claim of the Issuer against the 
    ' Certificate of STS-B. 
    Private Function IssuedBySTS_B(ByVal myClaimSet As ClaimSet) As Boolean
        Dim issuerClaimSet = myClaimSet.Issuer
        If issuerClaimSet Is Nothing Then
            Return False
        End If
        If issuerClaimSet.Count <> 1 Then
            Return False
        End If
        Dim issuerClaim = issuerClaimSet(0)
        If issuerClaim.ClaimType <> ClaimTypes.Thumbprint Then
            Return False
        End If
        If issuerClaim.Resource Is Nothing Then
            Return False
        End If
        Dim claimThumbprint() = CType(issuerClaim.Resource, Byte())
        ' It is assumed that stsB_Certificate is a variable of type 
        ' X509Certificate2 that is initialized with the Certificate of 
        ' STS-B.
        Dim stsB_Certificate = GetStsBCertificate()
        Dim certThumbprint() = stsB_Certificate.GetCertHash()
        If claimThumbprint.Length <> certThumbprint.Length Then
            Return False
        End If
        For i = 0 To claimThumbprint.Length - 1
            If claimThumbprint(i) <> certThumbprint(i) Then
                Return False
            End If
        Next i
        Return True
    End Function

설명

일반적으로 애플리케이션은 이 메서드 대신 CheckAccessCore를 재정의합니다.

애플리케이션에서 결과 CheckAccess에 대해 다른 정책 집합을 연결하거나 도입할 경우 또는 다른 정책 평가(체인) 모델을 제공할 경우 ServiceSecurityContext를 재정의합니다.

이 메서드는 CheckAccessCore를 호출합니다.

적용 대상

CheckAccess(OperationContext, Message)

메시지에 액세스해야 할 때 특정 작업 컨텍스트에 대한 권한 부여를 확인합니다.

public:
 virtual bool CheckAccess(System::ServiceModel::OperationContext ^ operationContext, System::ServiceModel::Channels::Message ^ % message);
public virtual bool CheckAccess (System.ServiceModel.OperationContext operationContext, ref System.ServiceModel.Channels.Message message);
abstract member CheckAccess : System.ServiceModel.OperationContext * Message -> bool
override this.CheckAccess : System.ServiceModel.OperationContext * Message -> bool
Public Overridable Function CheckAccess (operationContext As OperationContext, ByRef message As Message) As Boolean

매개 변수

message
Message

권한 부여를 확인하기 위해 검사할 Message입니다.

반환

Boolean

액세스 권한이 부여되면 true이고, 그렇지 않으면 false입니다. 기본값은 true입니다.

예제

다음 코드에서는 메시지 본문 액세스가 필요한 사용자 지정 액세스 제어 요구 사항을 적용하기 위해 이 메서드를 재정의하는 방법을 보여 줍니다.

public class myService_M_AuthorizationManager : ServiceAuthorizationManager
{
    // set max size for message
    int someMaxSize = 16000;
    protected override bool CheckAccessCore(OperationContext operationContext, ref Message message)
    {
        bool accessAllowed = false;
        MessageBuffer requestBuffer = message.CreateBufferedCopy(someMaxSize);

        // do access checks using the message parameter value and set accessAllowed appropriately
        if (accessAllowed)
        {
            // replace incoming message with fresh copy since accessing the message consumes it
            message = requestBuffer.CreateMessage();
        }
        return accessAllowed;
    }
}
Public Class myService_M_AuthorizationManager
    Inherits ServiceAuthorizationManager

    ' set max size for message
    Private someMaxSize As Integer = 16000

    Public Overrides Function CheckAccess(ByVal operationContext As OperationContext, _
                                          ByRef message As Message) As Boolean
        Dim accessAllowed = False
        Dim requestBuffer = Message.CreateBufferedCopy(someMaxSize)

        ' do access checks using the message parameter value and set accessAllowed appropriately
        If accessAllowed Then
            ' replace incoming message with fresh copy since accessing the message consumes it
            Message = requestBuffer.CreateMessage()
        End If
        Return accessAllowed
    End Function
End Class

설명

일반적으로 애플리케이션은 이 메서드 대신 CheckAccessCore를 재정의합니다. 이 메서드는 메시지 본문에 따라 권한 부여 결정이 달라지는 경우에만 사용합니다. 성능 문제 때문에 가능하다면 권한 부여 결정 시 메시지 본문에 액세스하지 않아도 되도록 애플리케이션을 다시 디자인해야 합니다.

애플리케이션에서 결과 ServiceSecurityContextMessage에 대해 다른 정책 집합을 연결하거나 도입할 경우 또는 다른 정책 평가(체인) 모델을 제공할 경우 이 메서드를 재정의합니다.

이 메서드는 CheckAccessCore를 호출합니다.

적용 대상