Så här gör du: Undersöka säkerhetskontexten
När du programmerar WCF-tjänster (Windows Communication Foundation) kan du med tjänstsäkerhetskontexten fastställa information om klientautentiseringsuppgifter och anspråk som används för att autentisera med tjänsten. Detta görs med hjälp av egenskaperna för ServiceSecurityContext klassen.
Du kan till exempel hämta identiteten för den aktuella klienten med hjälp PrimaryIdentity av egenskapen eller WindowsIdentity . Använd egenskapen för att avgöra om klienten är anonym IsAnonymous .
Du kan också avgöra vilka anspråk som görs för klientens räkning genom att iterera genom insamling av anspråk i AuthorizationContext egenskapen.
Hämta den aktuella säkerhetskontexten
- Få åtkomst till den statiska egenskapen Current för att hämta den aktuella säkerhetskontexten. Granska någon av egenskaperna för den aktuella kontexten från referensen.
Så här fastställer du anroparens identitet
- Skriv ut värdet för PrimaryIdentity egenskaperna och WindowsIdentity .
Så här parsar du anspråken för en anropare
Returnera den aktuella AuthorizationContext klassen. Använd egenskapen Current för att returnera den aktuella tjänstsäkerhetskontexten
AuthorizationContext
AuthorizationContext och returnera sedan egenskapen using.Parsa samlingen av ClaimSet objekt som returneras av ClaimSets egenskapen för AuthorizationContext klassen.
Exempel
I följande exempel skrivs värdena WindowsIdentity för egenskaperna och PrimaryIdentity ut för den aktuella säkerhetskontexten ClaimType och egenskapen, resursvärdet för anspråket och Right egenskapen för varje anspråk i den aktuella säkerhetskontexten.
// 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
Kompilera koden
Koden använder följande namnområden: