MessageQueuePermission 类

定义

允许用于消息的代码访问权限的控制。

public ref class MessageQueuePermission sealed : System::Security::CodeAccessPermission, System::Security::Permissions::IUnrestrictedPermission
[System.Serializable]
public sealed class MessageQueuePermission : System.Security.CodeAccessPermission, System.Security.Permissions.IUnrestrictedPermission
[<System.Serializable>]
type MessageQueuePermission = class
    inherit CodeAccessPermission
    interface IUnrestrictedPermission
Public NotInheritable Class MessageQueuePermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
继承
MessageQueuePermission
属性
实现

示例

下面的代码示例创建 的 MessageQueuePermission 多个实例,并演示如何使用 PermissionEntries 属性。


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.
    string queueCategory = queue.Category.ToString();
    string machineName = queue.MachineName;
    string label = queue.Label;
        MessageQueuePermission permission = new MessageQueuePermission(
            MessageQueuePermissionAccess.Receive,
            machineName,
            label,
            queueCategory);
    }

    // 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.
    string machineName = queue.MachineName;
    string label = queue.Label;
        entries[0] = new MessageQueuePermissionEntry(
            MessageQueuePermissionAccess.Receive,
            machineName,
            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.
    string machineName = queue.MachineName;
    string label = queue.Label;
        entries[0] = new MessageQueuePermissionEntry(
            MessageQueuePermissionAccess.Receive,
            machineName,
            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());
        }
    }
}

构造函数

MessageQueuePermission()

初始化 MessageQueuePermission 类的新实例。

MessageQueuePermission(MessageQueuePermissionAccess, String)

使用指定的访问级别和队列路径初始化 MessageQueuePermission 类的新实例。

MessageQueuePermission(MessageQueuePermissionAccess, String, String, String)

使用指定的访问级别、要使用的计算机、队列说明和队列类别初始化 MessageQueuePermission 类的新实例。

MessageQueuePermission(MessageQueuePermissionEntry[])

使用指定的权限访问级别项初始化 MessageQueuePermission 类的新实例。

MessageQueuePermission(PermissionState)

初始化具有指定权限状态的 MessageQueuePermission 类的一个新实例。

属性

PermissionEntries

获取此权限请求的权限项集合。

方法

Assert()

声明调用代码能够通过调用此方法的代码,访问受权限请求保护的资源,即使未对堆栈中处于较高位置的调用方授予访问该资源的权限。 使用 Assert() 会引起安全问题。

(继承自 CodeAccessPermission)
Copy()

创建并返回与当前权限对象相同的副本。

Demand()

如果未对调用堆栈中处于较高位置的所有调用方授予当前实例所指定的权限,则在运行时强制 SecurityException

(继承自 CodeAccessPermission)
Deny()
已过时.
已过时.

防止处于调用堆栈较高位置的调用函数使用可以调用此方法来访问当前实例指定资源的代码。

(继承自 CodeAccessPermission)
Equals(Object)

确定指定的 CodeAccessPermission 对象是否等于当前的 CodeAccessPermission

(继承自 CodeAccessPermission)
FromXml(SecurityElement)

通过 XML 编码重新构造具有指定状态的安全对象。

GetHashCode()

获取 CodeAccessPermission 对象的哈希代码,此代码适合在哈希算法和数据结构(例如哈希表)中使用。

(继承自 CodeAccessPermission)
GetType()

获取当前实例的 Type

(继承自 Object)
Intersect(IPermission)

返回表示当前权限对象和指定权限对象的交集的新权限对象。

IsSubsetOf(IPermission)

返回一个值,该值指示当前的权限对象是否为指定权限对象的一个子集。

IsUnrestricted()

返回一个值,该值指示在不了解权限语义的情况下是否可以将权限表示为无限制。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
PermitOnly()

防止处于调用堆栈较高位置的调用函数使用此代码,此代码将调用此方法来访问除当前实例指定的资源以外的所有资源。

(继承自 CodeAccessPermission)
ToString()

创建并返回当前权限对象的字符串表示形式。

(继承自 CodeAccessPermission)
ToXml()

创建安全对象及其当前状态的 XML 编码。

Union(IPermission)

返回一个新的权限对象,它是当前权限对象和指定权限对象的并集。

适用于

另请参阅