Cómo: Examinar el contexto de seguridad

Al programar servicios Windows Communication Foundation (WCF), el contexto de seguridad de servicio le permite determinar los detalles sobre las demandas y credenciales del cliente y utilizadas para autenticarse con el servicio. Esto se hace utilizando las propiedades de la clase ServiceSecurityContext.

Por ejemplo, puede recuperar la identidad del cliente actual utilizando la propiedad PrimaryIdentity o WindowsIdentity. Para determinar si el cliente es anónimo, utilice la propiedad IsAnonymous.

También puede determinar qué demandas se están realizando en nombre del cliente recorriendo en iteraciones la colección de demandas en la propiedad AuthorizationContext.

Obtención del contexto de seguridad actual

  • Tenga acceso a la propiedad estática Current para obtener el contexto de seguridad actual. Examine cualquiera de las propiedades del contexto actual de la referencia.

Determinación de la identidad del llamador

  1. Imprima el valor de las propiedades PrimaryIdentity y WindowsIdentity.

Análisis de las demandas de un llamador

  1. Devuelva la clase AuthorizationContext actual. Utilice la propiedad Current para devolver el contexto de seguridad de servicio actual, a continuación, devuelva el AuthorizationContext mediante la propiedad AuthorizationContext.

  2. Analice la colección de objetos de vueltos de ClaimSet mediante la propiedad ClaimSets de la clase AuthorizationContext.

Ejemplo

El ejemplo siguiente imprime los valores de las propiedades WindowsIdentity y PrimaryIdentity del contexto de seguridad actual y la propiedad ClaimType, el valor de recurso de la demanda, y la propiedad Right de cada demanda en el contexto de seguridad actual.

' Run this method from within a method protected by the PrincipalPermissionAttribute
' to see the security context data, including the primary identity.
Public Sub WriteServiceSecurityContextData(ByVal fileName As String) 
    Dim sw As New StreamWriter(fileName)
    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)
        sw.WriteLine()
        ' 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}", claim.ClaimType)
                sw.WriteLine(vbTab + " Resource = {0}", claim.Resource.ToString())
                sw.WriteLine(vbTab + " Right = {0}", claim.Right)
            Next claim
        Next claimset
    Finally
        sw.Dispose()
    End Try

End Sub 
// Run this method from within a method protected by the PrincipalPermissionAttribute
// to see the security context data, including the primary identity.
public void WriteServiceSecurityContextData(string fileName)
{
    using (StreamWriter sw = new StreamWriter(fileName))
    {
        // 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);
        sw.WriteLine();
        // 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}", claim.ClaimType);
                sw.WriteLine("\t Resource = {0}", claim.Resource.ToString());
                sw.WriteLine("\t Right = {0}", claim.Right);
            }
        }
    }
}

Compilar el código

El código utiliza los espacios de nombres siguientes:

Vea también

Conceptos

Seguridad de servicios
Identidad del servicio y autenticación