ServiceSecurityContext Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mewakili konteks keamanan pihak jarak jauh. Pada klien, mewakili identitas layanan dan, pada layanan, mewakili identitas klien.
public ref class ServiceSecurityContext
public class ServiceSecurityContext
type ServiceSecurityContext = class
Public Class ServiceSecurityContext
- Warisan
-
ServiceSecurityContext
Contoh
Contoh berikut menggunakan ServiceSecurityContext kelas untuk memberikan informasi tentang konteks keamanan saat ini. Kode membuat instans StreamWriter kelas untuk menulis informasi ke file.
// When this method runs, the caller must be an authenticated user
// and the ServiceSecurityContext is not a null instance.
public double Add(double n1, double n2)
{
// Write data from the ServiceSecurityContext to a file using the StreamWriter class.
using (StreamWriter sw = new StreamWriter(@"c:\ServiceSecurityContextInfo.txt"))
{
// 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);
// 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}, Resource: {1} Right: {2}",
claim.ClaimType,
claim.Resource.ToString(),
claim.Right);
sw.WriteLine();
}
}
}
return n1 + n2;
}
' When this method runs, the caller must be an authenticated user and the ServiceSecurityContext
' is not a null instance.
Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
' Write data from the ServiceSecurityContext to a file using the StreamWriter class.
Dim sw As New StreamWriter("c:\ServiceSecurityContextInfo.txt")
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)
' 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}, Resource: {1} Right: {2}", _
claim.ClaimType, _
claim.Resource.ToString(), _
claim.Right)
sw.WriteLine()
Next claim
Next claimset
Finally
sw.Dispose()
End Try
Return n1 + n2
End Function
Contoh berikut menunjukkan implementasi CheckAccessCore metode yang menggunakan untuk mengurai ServiceSecurityContext serangkaian klaim.
public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
// Extract the action URI from the OperationContext. Match this against the claims
// in the AuthorizationContext.
string action = operationContext.RequestContext.RequestMessage.Headers.Action;
Console.WriteLine("action: {0}", action);
// Iterate through the various claimsets in the AuthorizationContext.
foreach(ClaimSet cs in operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets)
{
// Examine only those claim sets issued by System.
if (cs.Issuer == ClaimSet.System)
{
// Iterate through claims of type "http://example.org/claims/allowedoperation".
foreach (Claim c in cs.FindClaims("http://example.org/claims/allowedoperation",
Rights.PossessProperty))
{
// Write the Claim resource to the console.
Console.WriteLine("resource: {0}", c.Resource.ToString());
// If the Claim resource matches the action URI then return true to allow access.
if (action == c.Resource.ToString())
return true;
}
}
}
// If this point is reached, return false to deny access.
return false;
}
}
Public Class MyServiceAuthorizationManager
Inherits ServiceAuthorizationManager
Protected Overrides Function CheckAccessCore(ByVal operationContext As OperationContext) As Boolean
' Extract the action URI from the OperationContext. Match this against the claims
' in the AuthorizationContext.
Dim action As String = operationContext.RequestContext.RequestMessage.Headers.Action
Console.WriteLine("action: {0}", action)
' Iterate through the various claimsets in the authorizationcontext.
Dim cs As ClaimSet
For Each cs In operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets
' Examine only those claim sets issued by System.
If cs.Issuer Is ClaimSet.System Then
' Iterate through claims of type "http://example.org/claims/allowedoperation".
Dim c As Claim
For Each c In cs.FindClaims("http://example.org/claims/allowedoperation", _
Rights.PossessProperty)
' Write the Claim resource to the console.
Console.WriteLine("resource: {0}", c.Resource.ToString())
' If the Claim resource matches the action URI then return true to allow access.
If action = c.Resource.ToString() Then
Return True
End If
Next c
End If
Next cs
' If we get here, return false, denying access.
Return False
End Function
End Class
Keterangan
Data adalah bagian SecurityMessageProperty dari untuk pesan.
Gunakan kelas ini untuk mendapatkan informasi tentang konteks keamanan jarak jauh saat runtime. Konteks keamanan dibuat ketika klien berhasil diautentikasi dan diotorisasi untuk mengakses metode. Ketika pesan berhasil diautentikasi dan diotorisasi, informasi keamanan dari klien dan untuk instans layanan saat ini dapat diperoleh dari instans kelas ini.
Anda dapat mengambil instans ServiceSecurityContext dari Current properti OperationContext kelas, atau menggunakannya dari dalam metode operasi layanan, seperti yang ditunjukkan dalam contoh berikut.
Mengurai ClaimSet
Penggunaan umum kelas adalah untuk mengambil serangkaian klaim saat ini untuk tujuan mengidentifikasi atau mengotorisasi klien saat mengakses metode. Kelas ClaimSet berisi kumpulan Claim objek, dan masing-masing dapat diurai untuk menentukan apakah klaim tertentu ada. Jika klaim yang ditentukan disediakan, otorisasi dapat diberikan. Fungsionalitas ini disediakan dengan mengambil CheckAccessCore alih metode ServiceAuthorizationManager kelas. Untuk contoh lengkapnya, lihat Kebijakan Otorisasi.
Mode Cookie dan IsAuthenticated
Perhatikan bahwa dalam beberapa keadaan, IsAuthenticated properti IIdentity antarmuka mengembalikan true bahkan jika klien jarak jauh diautentikasi sebagai pengguna anonim. (Properti PrimaryIdentity mengembalikan implementasi IIdentity antarmuka.) Keadaan berikut harus benar agar hal ini terjadi:
Layanan ini menggunakan autentikasi Windows.
Layanan ini memungkinkan masuk anonim.
Pengikatan adalah <customBinding>.
Pengikatan kustom mencakup
<security>elemen.Elemen ini
<security>mencakup <secureConversationBootstrap> dengan atribut yangrequireSecurityContextCancellationdiatur kefalse.
Konstruktor
| Nama | Deskripsi |
|---|---|
| ServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>) |
Menginisialisasi instans ServiceSecurityContext baru kelas dengan parameter otorisasi dan pengumpulan kebijakan yang ditentukan. |
| ServiceSecurityContext(AuthorizationContext) |
Menginisialisasi instans ServiceSecurityContext baru kelas dengan parameter otorisasi yang ditentukan. |
| ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>) |
Menginisialisasi instans ServiceSecurityContext baru kelas dengan kumpulan objek kebijakan. |
Properti
| Nama | Deskripsi |
|---|---|
| Anonymous |
Mengembalikan instans ServiceSecurityContext kelas yang berisi kumpulan klaim, identitas, dan data konteks kosong lainnya yang biasanya digunakan untuk mewakili pihak anonim. |
| AuthorizationContext |
Mendapatkan informasi otorisasi untuk instans kelas ini. AuthorizationContext berisi kumpulan ClaimSet bahwa aplikasi dapat menginterogasi dan mengambil informasi pihak. |
| AuthorizationPolicies |
Mendapatkan kumpulan kebijakan yang terkait dengan instans kelas ini. |
| Current |
Mendapatkan .ServiceSecurityContext |
| IsAnonymous |
Mendapatkan nilai yang menunjukkan apakah klien saat ini telah memberikan kredensial ke layanan. |
| PrimaryIdentity |
Mendapatkan identitas utama yang terkait dengan pengaturan saat ini. |
| WindowsIdentity |
Mendapatkan identitas Windows dari pengaturan saat ini. |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |