Nota
L'accés a aquesta pàgina requereix autorització. Podeu provar d'iniciar la sessió o de canviar els directoris.
L'accés a aquesta pàgina requereix autorització. Podeu provar de canviar els directoris.
Al programar servicios de Windows Communication Foundation (WCF), el contexto de seguridad del servicio permite identificar detalles sobre las credenciales del cliente y las reclamaciones utilizadas para autenticarse con el servicio. Esto se hace mediante las propiedades de la ServiceSecurityContext clase .
Por ejemplo, puede recuperar la identidad del cliente actual mediante la PrimaryIdentity propiedad o WindowsIdentity . Para determinar si el cliente es anónimo, use la IsAnonymous propiedad .
También puede determinar qué reclamaciones se realizan en nombre del cliente iterando a través de la colección de reclamaciones en la propiedad AuthorizationContext.
Para obtener el contexto de seguridad actual
- Acceda a la propiedad Current estática para obtener el contexto de seguridad actual. Examine cualquiera de las propiedades del contexto actual de la referencia.
Para determinar la identidad del autor de la llamada
- Imprima el valor de las PrimaryIdentity propiedades y WindowsIdentity .
Para analizar las reclamaciones de un llamante
Devuelve la clase actual AuthorizationContext . Use la Current propiedad para devolver el contexto de seguridad del servicio actual y, a continuación, devuelva el
AuthorizationContextmediante la AuthorizationContext propiedad .Analice la colección de objetos
devueltos por la propiedad de la clase .
Ejemplo
En el ejemplo siguiente se imprimen los valores de las propiedades WindowsIdentity y PrimaryIdentity del contexto de seguridad actual, así como la propiedad ClaimType, el valor de recurso de la reclamación, y la propiedad Right de cada reclamación 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 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);
}
}
}
}
' 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
Compilar el código
El código usa los siguientes espacios de nombres: