ServiceSecurityContext Klass

Definition

Representerar säkerhetskontexten för en fjärransluten part. På klienten representerar tjänstidentiteten och representerar klientidentiteten i tjänsten.

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

Exempel

I följande exempel används ServiceSecurityContext klassen för att ge information om den aktuella säkerhetskontexten. Koden skapar en instans av StreamWriter klassen för att skriva informationen till en fil.

// 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

I följande exempel visas en implementering av metoden CheckAccessCore som använder ServiceSecurityContext för att parsa en uppsättning anspråk.

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

Kommentarer

Data är en del av SecurityMessageProperty för ett meddelande.

Använd den här klassen för att hämta information om en fjärrsäkerhetskontext vid körning. En säkerhetskontext skapas när en klient har autentiserats och har behörighet att komma åt en metod. När ett meddelande har autentiserats och auktoriserats kan säkerhetsinformationen från klienten och för den aktuella tjänstinstansen hämtas från en instans av den här klassen.

Du kan hämta en instans av ServiceSecurityContext från Current -egenskapen för OperationContext klassen eller använda den inifrån en tjänståtgärdsmetod, som du ser i följande exempel.

Parsa en ClaimSet

En vanlig användning av klassen är att hämta den aktuella uppsättningen anspråk i syfte att identifiera eller auktorisera en klient vid åtkomst till en metod. Klassen ClaimSet innehåller en samling Claim objekt och var och en kan parsas för att avgöra om ett specifikt anspråk finns. Om det angivna anspråket anges kan auktorisering beviljas. Den här funktionen tillhandahålls genom att CheckAccessCore åsidosätta -metoden för ServiceAuthorizationManager klassen. Ett fullständigt exempel finns i auktoriseringsprincipen.

Observera att under vissa omständigheter returnerar IsAuthenticatedtrue egenskapen för IIdentity gränssnittet även om fjärrklienten autentiseras som en anonym användare. (Egenskapen PrimaryIdentity returnerar en implementering av IIdentity gränssnittet.) Följande omständigheter måste vara uppfyllda för att detta ska inträffa:

  • Tjänsten använder Windows authentication.

  • Tjänsten tillåter anonym inloggning.

  • Bindningen är en <customBinding>.

  • Den anpassade bindningen innehåller ett <security> element.

  • Elementet <security> innehåller en <secureConversationBootstrap> med requireSecurityContextCancellation attributet inställt på false.

Konstruktorer

Name Description
ServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>)

Initierar en ny instans av ServiceSecurityContext klassen med de angivna auktoriseringsparametrarna och en samling principer.

ServiceSecurityContext(AuthorizationContext)

Initierar en ny instans av ServiceSecurityContext klassen med de angivna auktoriseringsparametrarna.

ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>)

Initierar en ny instans av ServiceSecurityContext klassen med samlingen med principobjekt.

Egenskaper

Name Description
Anonymous

Returnerar en instans av ServiceSecurityContext klassen som innehåller en tom samling anspråk, identiteter och andra kontextdata som vanligtvis används för att representera en anonym part.

AuthorizationContext

Hämtar auktoriseringsinformationen för en instans av den här klassen. Innehåller AuthorizationContext en samling av ClaimSet som programmet kan förhöra och hämta information om parten.

AuthorizationPolicies

Hämtar samlingen med principer som är associerade med en instans av den här klassen.

Current

Hämtar den aktuella ServiceSecurityContext.

IsAnonymous

Hämtar ett värde som anger om den aktuella klienten har angett autentiseringsuppgifter till tjänsten.

PrimaryIdentity

Hämtar den primära identiteten som är associerad med den aktuella inställningen.

WindowsIdentity

Hämtar den aktuella inställningens Windows identitet.

Metoder

Name Description
Equals(Object)

Avgör om det angivna objektet är lika med det aktuella objektet.

(Ärvd från Object)
GetHashCode()

Fungerar som standard-hash-funktion.

(Ärvd från Object)
GetType()

Hämtar den aktuella instansen Type .

(Ärvd från Object)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
ToString()

Returnerar en sträng som representerar det aktuella objektet.

(Ärvd från Object)

Gäller för

Se även