Güvenli Mesaj Kullanımı

Güvenli Mesaj Kullanımı Güvenli Mesaj KullanımıBu makalemizde güvenli mesajlaşma altyapısının kullanımını sağlayan kod erişim güvenliği sınıfını inceleyeceğiz. Zorluk Derecesi: 1 2 3 4 5 Anahtar Sözcükler: Güvenli Mesaj Altyapısı, Kod Erişim Güvenliği, İzin, Microsoft .NET Microsoft .NET Framework’te mesajlaşma için kod erişimlerini kontrol edebilmek için MessageQueuePermission sınıfı tanımlanmıştır. System.Messaging altında yer alan MessageQueuePermission sınıfı CodeAccessPermission sınıfından türeyen, IUnrestrictedPermission arayüzünü destekleyen serileştirilebilir mühürlü bir sınıftır. Mühürlü sınıf, hiç bir sınıfın kendisinden türetilemeyeceğini ifade eder. Bu sınıfın erişilebilir tek bir özelliği vardır; PermissionEntries, bu izin nesnesi için izin kayıtlarının bir koleksiyonudur.Bu sınıfın erişilebilir metotları ise şunlardır; Assert, bu kodu çağıran kodun, erişimin bir yetki ile sınırlandırıldığı kaynağa her şekilde erişebileceğini ifadeeder. Bu kodun daha üst seviyesinde bu kodu çağıran tüm kodlara şartsız güvenilmesini sağlar. Assert kullanımı çoğu zaman güvenlik açıklarına yol açabilir, zorunlu kalınmadıkça kullanımı önerilmez. Copy, şu an ki izin nesnesinin birebir bir yeni bir kopyasını oluşturup geri döndürür. Demand, bu kodu çağıran kodların bu yetkiye sahip olup olmadığının denetimi için CLR’yi zorlar, eğer bu kodu çağıran kodlardan herhangi bir seviyede o yetki sağlanmamış ise güvenlik ihlalini belirtmek için SecurityException istisna fırlatılır. Deny, çağırma yığıtındaki bu kodu çağıran kodların bu kodu çağırmalarını engeller. Equals, iki nesnenin birbirine eşit olup olmadığını test eder. FromXml, ToXml metotuyla XML olarak ifade edilen nesneyi yeniden oluşturur. XML Serileştirme gibidir. GetHashCode, bellekteki o nesneye özgü bir hash kodu oluşturur. GetType, bu nesnenin tipini verir. Intersect, şu an ki izin nesnesiyle, belirtilen izinin kesişimini ifade eden başka bir izin nesnesi oluşturur. IsSubsetOf, şu an ki izin nesnesinin, belirtilen izinin bir alt kümesi olup olmadığını ifade eder. PermitOnly, bu metotu çağıran kodların bu izin nesnesi tarafından sınırlandırılmış kaynak dışındaki sınırlı kaynaklara erişmesini engeller. ToString, şu an ki nesneyi ifade eden bir metin oluşturur. ToXml, bu nesnenin tüm durumnu ifade FromXML metotuyla yeniden olşturulabilinecek bir XML oluşturur. Union, şu an ki izin nesnesiyle, belirtilen izinin birleşimini ifade eden başka bir izin nesnesi oluşturur.Şimdi de MessageQueuePermission sınıfının basit bir kullanımı örnekleyelim. Bu örnek MSDN"den birebir alınmıştır.C#using System; using System.Messaging; public class MessageQueuePermissionExample { public static void Main() { // Create a new instance of the class. MessageQueuePermissionExample example = new MessageQueuePermissionExample(); // Create a message queue on the local computer. CreateQueue(".\exampleQueue", false); // Demonstrate MessageQueuePermission"s constructors. example.CreatePermission1(); example.CreatePermission2(); example.CreatePermission3(); example.CreatePermission4(); example.CreatePermission5(); // Get and set MessageQueuePermission"s PermissionEntries property. example.GetPermissionEntries(); } // Creates a new queue. public static void CreateQueue(string queuePath, bool transactional) { if (!MessageQueue.Exists(queuePath)) { MessageQueue.Create(queuePath, transactional); } else { Console.WriteLine(queuePath + " already exists."); } } // Demonstrates the following MessageQueuePermission constructor: // public #ctor () public void CreatePermission1() { // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission(); } // Demonstrates the following MessageQueuePermission constructor: // public #ctor (MessageQueuePermissionAccess permissionAccess, // String path) public void CreatePermission2() { // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission( MessageQueuePermissionAccess.Receive, ".\exampleQueue"); } // Demonstrates the following MessageQueuePermission constructor: // public #ctor (MessageQueuePermissionAccess permissionAccess, // String machineName, String label, String category) public void CreatePermission3() { // Connect to a queue on the local computer. MessageQueue queue = new MessageQueue(".\exampleQueue"); // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission( MessageQueuePermissionAccess.Receive, queue.MachineName, queue.Label, queue.Category.ToString()); } // Demonstrates the following MessageQueuePermission constructor: //public #ctor (MessageQueuePermissionEntry[] permissionAccessEntries) public void CreatePermission4() { // Connect to a queue on the local computer. MessageQueue queue = new MessageQueue(".\exampleQueue"); // Create an array of type MessageQueuePermissionEntry. MessageQueuePermissionEntry[] entries = _ new MessageQueuePermissionEntry[1]; // Create a new instance of MessageQueuePermissionEntry and place the // instance in the array. entries[0] = new MessageQueuePermissionEntry( MessageQueuePermissionAccess.Receive, queue.MachineName, queue.Label, queue.Category.ToString()); // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission( entries); } // Demonstrates the following MessageQueuePermission constructor: //public #ctor (PermissionState state) public void CreatePermission5() { // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission( System.Security.Permissions.PermissionState.Unrestricted); } public void GetPermissionEntries() { // Connect to a queue on the local computer. MessageQueue queue = new MessageQueue(".\exampleQueue"); // Create an array of type MessageQueuePermissionEntry. MessageQueuePermissionEntry[] entries = new MessageQueuePermissionEntry[1]; // Create a new instance of MessageQueuePermissionEntry and place the // instance in the array. entries[0] = new MessageQueuePermissionEntry( MessageQueuePermissionAccess.Receive, queue.MachineName, queue.Label, queue.Category.ToString()); // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission( entries); // Create a new instance of MessageQueuePermissionEntryCollection and // use it to retrieve the permission"s PermissionEntries property // value. MessageQueuePermissionEntryCollection collection = permission.PermissionEntries; // Loop through the collection. foreach (MessageQueuePermissionEntry entry in collection) { // Display the property values of each MessageQueuePermissionEntry. Console.WriteLine("PermissionAccess: {0}", entry.PermissionAccess); Console.WriteLine("MachineName: {0}", entry.MachineName); Console.WriteLine("Label: {0}", entry.Label); Console.WriteLine("Category: {0}", entry.Category.ToString()); } } }VB.NETImports System Imports System.Messaging Module MessageQueuePermissionAccessExample Sub Main() " Create a message queue on the local computer. CreateQueue(".\exampleQueue", False) " Demonstrate MessageQueuePermission"s constructors. CreatePermission1() CreatePermission2() CreatePermission3() CreatePermission4() CreatePermission5() " Get and set MessageQueuePermission"s PermissionEntries property. GetPermissionEntries() End Sub " Creates a new queue. Public Sub CreateQueue(ByVal queuePath As String, _ ByVal transactional As Boolean) If Not MessageQueue.Exists(queuePath) Then MessageQueue.Create(queuePath, transactional) Else Console.WriteLine(queuePath & " already exists.") End If End Sub " Demonstrates the following MessageQueuePermission constructor: " public #ctor () Public Sub CreatePermission1() " Create a new instance of MessageQueuePermission. Dim permission As New MessageQueuePermission End Sub " Demonstrates the following MessageQueuePermission constructor: " public #ctor (MessageQueuePermissionAccess permissionAccess, " String path) Public Sub CreatePermission2() " Create a new instance of MessageQueuePermission. Dim permission As New MessageQueuePermission( _ MessageQueuePermissionAccess.Receive, ".\exampleQueue") End Sub " Demonstrates the following MessageQueuePermission constructor: " public #ctor (MessageQueuePermissionAccess permissionAccess, " String machineName, String label, String category) Public Sub CreatePermission3() " Connect to a queue on the local computer Dim queue As New MessageQueue(".\exampleQueue") " Create a new instance of MessageQueuePermission. Dim permission As New MessageQueuePermission( _ MessageQueuePermissionAccess.Receive, _ queue.MachineName, _ queue.Label, _ queue.Category.ToString()) End Sub " Demonstrates the following MessageQueuePermission constructor: " public #ctor (MessageQueuePermissionEntry[] permissionAccessEntries) Public Sub CreatePermission4() " Connect to a queue on the local computer Dim queue As New MessageQueue(".\exampleQueue") " Create an array of type MessageQueuePermissionEntry. Dim entries(1) As MessageQueuePermissionEntry " Create a new instance of MessageQueuePermissionEntry and place the " instance in the array. entries(0) = New MessageQueuePermissionEntry( _ MessageQueuePermissionAccess.Receive, _ queue.MachineName, _ queue.Label, _ queue.Category.ToString()) Dim permission As New MessageQueuePermission(entries) End Sub " Demonstrates the following MessageQueuePermission constructor: " public #ctor (PermissionState state) Public Sub CreatePermission5() " Create a new instance of MessageQueuePermission. Dim permission As New MessageQueuePermission( _ System.Security.Permissions.PermissionState.None) End Sub Public Sub GetPermissionEntries() " Connect to a queue on the local computer. Dim queue As New MessageQueue(".\exampleQueue") " Create an array of type MessageQueuePermissionEntry. Dim entries(1) As MessageQueuePermissionEntry " Create a new instance of MessageQueuePermissionEntry and place the " instance in the array. entries(0) = New MessageQueuePermissionEntry( _ MessageQueuePermissionAccess.Receive, _ queue.MachineName, _ queue.Label, _ queue.Category.ToString()) Dim permission As New MessageQueuePermission(entries) " Create a new instance of MessageQueuePermissionEntryCollection and " use it to retrieve the permission"s PermissionEntries property " value. Dim collection As MessageQueuePermissionEntryCollection collection = permission.PermissionEntries " Loop through the collection. For Each entry As MessageQueuePermissionEntry In collection " Display the property values of each MessageQueuePermissionEntry. Console.WriteLine("PermissionAccess: {0}", entry.PermissionAccess) Console.WriteLine("MachineName: {0}", entry.MachineName) Console.WriteLine("Label: {0}", entry.Label) Console.WriteLine("Category: {0}", entry.Category.ToString()) Next End Sub End ModuleJ#import System.*; import System.Messaging.*; public class MessageQueuePermissionExample { public static void main(String[] args) { // Create a new instance of the class. MessageQueuePermissionExample example = new MessageQueuePermissionExample(); // Create a message queue on the local computer. CreateQueue(".\exampleQueue", false); // Demonstrate MessageQueuePermission"s constructors. example.CreatePermission1(); example.CreatePermission2(); example.CreatePermission3(); example.CreatePermission4(); example.CreatePermission5(); // Get and set MessageQueuePermission"s PermissionEntries property. example.GetPermissionEntries(); } //main // Creates a new queue. public static void CreateQueue(String queuePath, boolean transactional) { if (!(MessageQueue.Exists(queuePath))) { MessageQueue.Create(queuePath, transactional); } else { Console.WriteLine(queuePath + " already exists."); } } //CreateQueue // Demonstrates the following MessageQueuePermission constructor: // public #ctor () public void CreatePermission1() { // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission(); } //CreatePermission1 // Demonstrates the following MessageQueuePermission constructor: // public #ctor (MessageQueuePermissionAccess permissionAccess, // String path) public void CreatePermission2() { // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission( MessageQueuePermissionAccess.Receive, ".\exampleQueue"); } //CreatePermission2 // Demonstrates the following MessageQueuePermission constructor: // public #ctor (MessageQueuePermissionAccess permissionAccess, // String machineName, String label, String category) public void CreatePermission3() { // Connect to a queue on the local computer. MessageQueue queue = new MessageQueue(".\exampleQueue"); // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission( MessageQueuePermissionAccess.Receive, queue.get_MachineName(), queue.get_Label(), queue.get_Category().ToString()); } //CreatePermission3 // Demonstrates the following MessageQueuePermission constructor: // public #ctor (MessageQueuePermissionEntry permissionAccessEntries[]) public void CreatePermission4() { // Connect to a queue on the local computer. MessageQueue queue = new MessageQueue(".\exampleQueue"); // Create an array of type MessageQueuePermissionEntry. MessageQueuePermissionEntry entries[] = new MessageQueuePermissionEntry[1]; // Create a new instance of MessageQueuePermissionEntry and place the // instance in the array. entries.set_Item(0, new MessageQueuePermissionEntry( MessageQueuePermissionAccess.Receive, queue.get_MachineName(), queue.get_Label(), queue.get_Category().ToString())); // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission(entries); } //CreatePermission4 // Demonstrates the following MessageQueuePermission constructor: // public #ctor (PermissionState state) public void CreatePermission5() { // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission(System.Security.Permissions. PermissionState.Unrestricted); } //CreatePermission5 public void GetPermissionEntries() { // Connect to a queue on the local computer. MessageQueue queue = new MessageQueue(".\exampleQueue"); // Create an array of type MessageQueuePermissionEntry. MessageQueuePermissionEntry entries[] = new MessageQueuePermissionEntry[1]; // Create a new instance of MessageQueuePermissionEntry and place the // instance in the array. entries.set_Item(0, new MessageQueuePermissionEntry( MessageQueuePermissionAccess.Receive, queue.get_MachineName(), queue.get_Label(), queue.get_Category().ToString())); // Create a new instance of MessageQueuePermission. MessageQueuePermission permission = new MessageQueuePermission(entries); // Create a new instance of MessageQueuePermissionEntryCollection and // use it to retrieve the permission"s PermissionEntries property // value. MessageQueuePermissionEntryCollection collection = permission.get_PermissionEntries(); // Loop through the collection. for (int iCtr = 0; iCtr < collection.get_Count(); iCtr++) { MessageQueuePermissionEntry entry = collection.get_Item(iCtr); // Display the property values of each MessageQueuePermissionEntry. Console.WriteLine("PermissionAccess: {0}", entry.get_PermissionAccess()); Console.WriteLine("MachineName: {0}", entry.get_MachineName()); Console.WriteLine("Label: {0}", entry.get_Label()); Console.WriteLine("Category: {0}", entry.get_Category().ToString()); } } //GetPermissionEntries } //MessageQueuePermissionExample Son Söz: Bir sonraki makalemizde görüşünceye kadar güvende kalın... İlgili Makaleler: Referanslar: Yazar : Yunus Emre ALPÖZENe-Posta : yunus.alpozen et msakademik.net

Döküman Arama

Başlık :

Kapat