Примечание.
Для доступа к этой странице требуется авторизация. Вы можете попробовать войти или изменить каталоги.
Для доступа к этой странице требуется авторизация. Вы можете попробовать изменить каталоги.
При программировании служб Windows Communication Foundation (WCF) контекст безопасности службы позволяет определить сведения о учетных данных клиента и утверждениях, используемых для проверки подлинности со службой. Это делается с помощью свойств ServiceSecurityContext класса.
Например, можно получить идентификатор текущего клиента, используя свойство PrimaryIdentity или свойство WindowsIdentity. Чтобы определить, является ли клиент анонимным, используйте IsAnonymous свойство.
Вы также можете определить, какие утверждения выдвигаются в пользу клиента, проводя итерацию по коллекции утверждений в свойстве AuthorizationContext.
Получение текущего контекста безопасности
- Доступ к статическому свойству Current для получения текущего контекста безопасности. Изучите любые свойства текущего контекста из ссылки.
Для определения личности звонящего
- Выведите значения свойств PrimaryIdentity и WindowsIdentity.
Для анализа утверждений вызывающей стороны
Возвращает текущий AuthorizationContext класс. Используйте свойство Current, чтобы вернуть текущий контекст безопасности службы, а затем с помощью свойства
AuthorizationContextвернуть AuthorizationContext.Проанализируйте коллекцию ClaimSet объектов, возвращаемых свойством ClaimSetsAuthorizationContext класса.
Пример
В следующем примере выводятся значения свойств WindowsIdentity и PrimaryIdentity текущего контекста безопасности, свойство ClaimType, представляющее значение ресурса утверждения, и свойство Right каждого утверждения в текущем контексте безопасности.
// 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
Компиляция кода
В коде используются следующие пространства имен: