Aracılığıyla paylaş


ServiceSecurityContext Sınıf

Tanım

Uzak bir tarafın güvenlik bağlamını temsil eder. İstemcide, hizmet kimliğini temsil eder ve hizmette istemci kimliğini temsil eder.

public ref class ServiceSecurityContext
public class ServiceSecurityContext
type ServiceSecurityContext = class
Public Class ServiceSecurityContext
Devralma
ServiceSecurityContext

Örnekler

Aşağıdaki örnek, geçerli güvenlik bağlamı ServiceSecurityContext hakkında bilgi sağlamak için sınıfını kullanır. Kod, bilgileri bir dosyaya StreamWriter yazmak için sınıfının bir örneğini oluşturur.

// When this method runs, the caller must be an authenticated user
// and the ServiceSecurityContext is not a null instance.
public double Add(double n1, double n2)
{
    // Write data from the ServiceSecurityContext to a file using the StreamWriter class.
    using (StreamWriter sw = new StreamWriter(@"c:\ServiceSecurityContextInfo.txt"))
    {
        // Write the primary identity and Windows identity. The primary identity is derived from
        // the credentials used to authenticate the user. The Windows identity may be a null string.
        sw.WriteLine("PrimaryIdentity: {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name);
        sw.WriteLine("WindowsIdentity: {0}", ServiceSecurityContext.Current.WindowsIdentity.Name);

        // Write the claimsets in the authorization context. By default, there is only one claimset
        // provided by the system.
        foreach (ClaimSet claimset in ServiceSecurityContext.Current.AuthorizationContext.ClaimSets)
        {
            foreach (Claim claim in claimset)
            {
                // Write out each claim type, claim value, and the right. There are two
                // possible values for the right: "identity" and "possessproperty".
                sw.WriteLine("Claim Type: {0}, Resource: {1} Right: {2}",
                    claim.ClaimType,
                    claim.Resource.ToString(),
                    claim.Right);
                sw.WriteLine();
            }
        }
    }
    return n1 + n2;
}
' When this method runs, the caller must be an authenticated user and the ServiceSecurityContext 
' is not a null instance. 
Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
    ' Write data from the ServiceSecurityContext to a file using the StreamWriter class.
    Dim sw As New StreamWriter("c:\ServiceSecurityContextInfo.txt")
    Try
        ' Write the primary identity and Windows identity. The primary identity is derived from 
        ' the credentials used to authenticate the user. The Windows identity may be a null string.
        sw.WriteLine("PrimaryIdentity: {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name)
        sw.WriteLine("WindowsIdentity: {0}", ServiceSecurityContext.Current.WindowsIdentity.Name)

        ' Write the claimsets in the authorization context. By default, there is only one claimset
        ' provided by the system. 
        Dim claimset As ClaimSet
        For Each claimset In ServiceSecurityContext.Current.AuthorizationContext.ClaimSets
            Dim claim As Claim
            For Each claim In claimset
                ' Write out each claim type, claim value, and the right. There are two
                ' possible values for the right: "identity" and "possessproperty". 
                sw.WriteLine("Claim Type: {0}, Resource: {1} Right: {2}", _
                claim.ClaimType, _
                claim.Resource.ToString(), _
                claim.Right)
                sw.WriteLine()
            Next claim
        Next claimset
    Finally
        sw.Dispose()
    End Try
    Return n1 + n2
End Function

Aşağıdaki örnekte, bir talep kümesini ayrıştırmak için kullanan ServiceSecurityContext yönteminin bir uygulaması CheckAccessCore gösterilmektedir.

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;
        Console.WriteLine("action: {0}", action);

        // Iterate through the various claimsets 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://example.org/claims/allowedoperation".
                foreach (Claim c in cs.FindClaims("http://example.org/claims/allowedoperation",
                    Rights.PossessProperty))
                {
                    // Write the Claim resource to the console.
                    Console.WriteLine("resource: {0}", c.Resource.ToString());

                    // 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
        Console.WriteLine("action: {0}", 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://example.org/claims/allowedoperation".
                Dim c As Claim
                For Each c In  cs.FindClaims("http://example.org/claims/allowedoperation", _
                        Rights.PossessProperty)
                    ' Write the Claim resource to the console.
                    Console.WriteLine("resource: {0}", c.Resource.ToString())
                    
                    ' 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 we get here, return false, denying access.
        Return False
    
    End Function 
End Class

Açıklamalar

Veriler iletinin SecurityMessageProperty bir parçasıdır.

Çalışma zamanında bir uzak güvenlik bağlamı hakkında bilgi almak için bu sınıfı kullanın. İstemcinin kimliği başarıyla doğrulandığında ve bir yönteme erişme yetkisine sahip olduğunda bir güvenlik bağlamı oluşturulur. bir iletinin kimliği başarıyla doğrulandığında ve yetkilendirildiğinde, istemciden ve geçerli hizmet örneği için güvenlik bilgileri bu sınıfın bir örneğinden alınabilir.

öğesinin ServiceSecurityContext bir örneğini sınıfının özelliğinden Current OperationContext alabilir veya aşağıdaki örnekte gösterildiği gibi bir hizmet işlemi yöntemi içinden kullanabilirsiniz.

ClaimSet Ayrıştırma

sınıfının yaygın bir kullanımı, bir yönteme erişirken bir istemciyi tanımlamak veya yetkilendirmek amacıyla geçerli talep kümesini almaktır. ClaimSet sınıfı bir nesne koleksiyonu içerir ve her biri belirli bir talebin Claim mevcut olup olmadığını belirlemek için ayrıştırılabilir. Belirtilen talep sağlanmışsa yetkilendirme verilebilir. Bu işlevsellik, sınıfının yöntemi ServiceAuthorizationManager geçersiz kılınarak CheckAccessCore sağlanır. Tam bir örnek için bkz . Yetkilendirme İlkesi.

Bazı durumlarda, IsAuthenticated uzak istemcinin kimliği anonim kullanıcı olarak doğrulanmış olsa bile arabiriminin IIdentity özelliğinin döndürüldüğünü true unutmayın. PrimaryIdentity(özelliği arabiriminin IIdentity bir uygulamasını döndürür.) Bunun gerçekleşmesi için aşağıdaki koşulların geçerli olması gerekir:

  • Hizmet Windows kimlik doğrulaması kullanır.

  • Hizmet anonim oturum açma işlemlerine izin verir.

  • Bağlama bir <customBinding'dir>.

  • Özel bağlama bir <security> öğe içerir.

  • öğesi, <security> özniteliği olarak ayarlanmış falsebir <secureConversationBootstrap> requireSecurityContextCancellation içerir.

Oluşturucular

ServiceSecurityContext(AuthorizationContext)

Belirtilen yetkilendirme parametreleriyle sınıfının yeni bir örneğini ServiceSecurityContext başlatır.

ServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>)

Belirtilen yetkilendirme parametreleri ve ilke koleksiyonu ile sınıfının yeni bir örneğini ServiceSecurityContext başlatır.

ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>)

İlke koleksiyonu nesnesiyle sınıfının yeni bir örneğini ServiceSecurityContext başlatır.

Özellikler

Anonymous

Genellikle anonim bir tarafı temsil etmek için kullanılan boş bir talep, kimlik ve diğer bağlam verileri koleksiyonunu içeren sınıfın bir örneğini ServiceSecurityContext döndürür.

AuthorizationContext

Bu sınıfın bir örneğinin yetkilendirme bilgilerini alır. , AuthorizationContext uygulamanın tarafın bilgilerini sorgulayıp alabildiği bir koleksiyonu ClaimSet içerir.

AuthorizationPolicies

Bu sınıfın bir örneğiyle ilişkili ilke koleksiyonunu alır.

Current

Geçerli ServiceSecurityContextöğesini alır.

IsAnonymous

Geçerli istemcinin hizmete kimlik bilgileri sağlayıp sağlamadığını gösteren bir değer alır.

PrimaryIdentity

Geçerli ayarla ilişkili birincil kimliği alır.

WindowsIdentity

Geçerli ayarın Windows kimliğini alır.

Yöntemler

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
ToString()

Geçerli nesneyi temsil eden dizeyi döndürür.

(Devralındığı yer: Object)

Şunlara uygulanır

Ayrıca bkz.