Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Ao programar serviços da Windows Communication Foundation (WCF), o contexto de segurança do serviço permite-lhe determinar detalhes sobre as credenciais do cliente e as reivindicações usadas para autenticar com o serviço. Isto é feito utilizando as propriedades da ServiceSecurityContext classe.
Por exemplo, pode recuperar a identidade do cliente atual usando a PrimaryIdentity ou a WindowsIdentity propriedade. Para determinar se o cliente é anónimo, utilize a IsAnonymous propriedade.
Também pode determinar que reclamações estão a ser feitas em nome do cliente, iterando a recolha das reclamações na AuthorizationContext propriedade.
Para perceber o contexto atual de segurança
- Acede à propriedade Current estática para obter o contexto de segurança atual. Examine qualquer uma das propriedades do contexto atual da referência.
Para determinar a identidade do chamador
- Imprima o valor das propriedades PrimaryIdentity e WindowsIdentity.
Para analisar as alegações de um chamador
Devolve a classe atual AuthorizationContext . Use a Current propriedade para devolver o contexto de segurança atual do serviço, depois retorne o
AuthorizationContextusando a AuthorizationContext propriedade.Analise a coleção de objetos ClaimSet devolvidos pela propriedade ClaimSets da classe AuthorizationContext.
Exemplo
O exemplo seguinte imprime os valores das propriedades WindowsIdentity e PrimaryIdentity do contexto de segurança atual, bem como a propriedade ClaimType, que representa o valor do recurso da reivindicação, e a propriedade Right de cada reivindicação no contexto de segurança atual.
// 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
Compilando o código
O código utiliza os seguintes espaços de nomes: