MessageQueuePermissionAttribute Class

Definition

Allows declarative MessageQueue permission checks.

public ref class MessageQueuePermissionAttribute : System::Security::Permissions::CodeAccessSecurityAttribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Event | System.AttributeTargets.Method | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)]
[System.Serializable]
public class MessageQueuePermissionAttribute : System.Security.Permissions.CodeAccessSecurityAttribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Event | System.AttributeTargets.Method | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)>]
[<System.Serializable>]
type MessageQueuePermissionAttribute = class
    inherit CodeAccessSecurityAttribute
Public Class MessageQueuePermissionAttribute
Inherits CodeAccessSecurityAttribute
Inheritance
Attributes

Examples

The following code example demonstrates the use of MessageQueuePermissionAttribute.


#using <System.Messaging.dll>
#using <System.dll>

using namespace System;
using namespace System::Messaging;

// Creates a new queue.
void CreateQueue(String^ queuePath, bool transactional)
{
    if (!MessageQueue::Exists(queuePath))
    {
        MessageQueue^ queue = MessageQueue::Create(queuePath, transactional);
        queue->Close();       
    }
    else
    {
        Console::WriteLine("{0} already exists.", queuePath);
    }
}

// Demonstrates the following MessageQueuePermissionAttribute constructor:
// public #ctor (SecurityAction action)
void CreateAttribute()
{

    // Create a new instance of MessageQueuePermissionAttribute.
    MessageQueuePermissionAttribute^ attribute =
        gcnew MessageQueuePermissionAttribute(
        System::Security::Permissions::SecurityAction::Assert);

}


void CategoryExample()
{

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // Create a new instance of MessageQueuePermissionAttribute.
    MessageQueuePermissionAttribute^ attribute =
        gcnew MessageQueuePermissionAttribute(
        System::Security::Permissions::SecurityAction::Assert);

    // Set the attribute's Category property value, based on the queue's
    // Category property value.
    attribute->Category = queue->Category.ToString();

    // Display the new value of the attribute's Category property.
    Console::WriteLine("attribute->Category: {0}",
        attribute->Category);

    queue->Close();
}

void LabelExample()
{

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // Create a new instance of MessageQueuePermissionAttribute.
    MessageQueuePermissionAttribute^ attribute =
        gcnew MessageQueuePermissionAttribute(
        System::Security::Permissions::SecurityAction::Assert);

    // Set the attribute's Label property value, based on the queue's Label
    // property value.
    attribute->Label = queue->Label;

    // Display the new value of the attribute's Label property.
    Console::WriteLine("attribute->Label: {0}", attribute->Label);

    queue->Close();
}


void MachineNameExample()
{

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // Create a new instance of MessageQueuePermissionAttribute.
    MessageQueuePermissionAttribute^ attribute =
        gcnew MessageQueuePermissionAttribute(
        System::Security::Permissions::SecurityAction::Assert);

    // Set the attribute's MachineName property value, based on the queue's
    // MachineName property value.
    attribute->MachineName = queue->MachineName;

    // Display the new value of the attribute's MachineName property.
    Console::WriteLine("attribute->MachineName: {0}",
        attribute->MachineName);

    queue->Close();
}

void PathExample()
{

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // Create a new instance of MessageQueuePermissionAttribute.
    MessageQueuePermissionAttribute^ attribute =
        gcnew MessageQueuePermissionAttribute(
        System::Security::Permissions::SecurityAction::Assert);

    // Set the attribute's Path property value, based on the queue's Path
    // property value.
    attribute->Path = queue->Path;

    // Display the new value of the attribute's Path property.
    Console::WriteLine("attribute->Path: {0}", attribute->Path);

    queue->Close();
}

void PermissionAccessExample()
{

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // Create a new instance of MessageQueuePermissionAttribute.
    MessageQueuePermissionAttribute^ attribute =
        gcnew MessageQueuePermissionAttribute(
        System::Security::Permissions::SecurityAction::Assert);

    // Set the attribute's PermissionAccess property value.
    attribute->PermissionAccess = MessageQueuePermissionAccess::Receive;

    // Display the new value of the attribute's PermissionAccess property.
    Console::WriteLine("attribute->PermissionAccess: {0}",
        attribute->PermissionAccess);

    queue->Close();
}

void CreatePermissionExample()
{

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // Create a new instance of MessageQueuePermissionAttribute.
    MessageQueuePermissionAttribute^ attribute =
        gcnew MessageQueuePermissionAttribute(
        System::Security::Permissions::SecurityAction::Assert);

    // Set the attribute's Path property value, based on the queue's Path
    // property value.
    attribute->Path = queue->Path;

    // Get an IPermission interface by calling the attribute's
    // CreatePermission() method.
    System::Security::IPermission^ permission = attribute->CreatePermission();

    queue->Close();
}

int main()
{
    try
    {

        // Create a non-transactional queue on the local computer.
        CreateQueue(".\\exampleQueue", false);

        // Demonstrate the members of MessageQueuePermissionAttribute.
        // Note that the Path, FormatName, MachineName, Label, and Category
        // property values cannot all be set on the same instance of
        // MessageQueuePermissionAttribute. Trying to do so will throw an
        // exception of type System.InvalidOperationException.

        CreateAttribute();
        CategoryExample();
        LabelExample();
        MachineNameExample();
        PathExample();
        PermissionAccessExample();
        CreatePermissionExample();
    }

    catch (InvalidOperationException^)
    {
        Console::WriteLine("Please install Message Queuing.");
    }

    catch (MessageQueueException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}

using System;
using System.Messaging;

public class MessageQueuePermissionAttributeExample
{
    public static void Main()
    {
        // Create a new instance of the class.
        MessageQueuePermissionAttributeExample example =
            new MessageQueuePermissionAttributeExample();

        // Create a non-transactional queue on the local computer.
        CreateQueue(".\\exampleQueue", false);

        // Demonstrate the members of MessageQueuePermissionAttribute.
        // Note that the Path, FormatName, MachineName, Label, and Category
        // property values cannot all be set on the same instance of
        // MessageQueuePermissionAttribute. Trying to do so will throw an
        // exception of type System.InvalidOperationException.
        example.CreateAttribute();
        example.CategoryExample();
        example.LabelExample();
        example.MachineNameExample();
        example.PathExample();
        example.PermissionAccessExample();
        example.CreatePermissionExample();
    }

    // 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 MessageQueuePermissionAttribute constructor:
    // public #ctor (SecurityAction action)
    public void CreateAttribute()
    {

        // Create a new instance of MessageQueuePermissionAttribute.
        MessageQueuePermissionAttribute attribute =
            new MessageQueuePermissionAttribute(
            System.Security.Permissions.SecurityAction.Assert);

    }

    public void CategoryExample()
    {

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Create a new instance of MessageQueuePermissionAttribute.
        MessageQueuePermissionAttribute attribute =
            new MessageQueuePermissionAttribute(
            System.Security.Permissions.SecurityAction.Assert);

        // Set the attribute's Category property value, based on the queue's
        // Category property value.
        attribute.Category = queue.Category.ToString();

        // Display the new value of the attribute's Category property.
        Console.WriteLine("attribute.Category: {0}",
            attribute.Category.ToString());

    }

    public void LabelExample()
    {

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Create a new instance of MessageQueuePermissionAttribute.
        MessageQueuePermissionAttribute attribute =
            new MessageQueuePermissionAttribute(
            System.Security.Permissions.SecurityAction.Assert);

        // Set the attribute's Label property value, based on the queue's Label
        // property value.
        attribute.Label = queue.Label;

        // Display the new value of the attribute's Label property.
        Console.WriteLine("attribute.Label: {0}", attribute.Label);

    }

    public void MachineNameExample()
    {

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Create a new instance of MessageQueuePermissionAttribute.
        MessageQueuePermissionAttribute attribute =
            new MessageQueuePermissionAttribute(
            System.Security.Permissions.SecurityAction.Assert);

        // Set the attribute's MachineName property value, based on the queue's
        // MachineName property value.
        attribute.MachineName = queue.MachineName;

        // Display the new value of the attribute's MachineName property.
        Console.WriteLine("attribute.MachineName: {0}",
            attribute.MachineName);

    }

    public void PathExample()
    {

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Create a new instance of MessageQueuePermissionAttribute.
        MessageQueuePermissionAttribute attribute =
            new MessageQueuePermissionAttribute(
            System.Security.Permissions.SecurityAction.Assert);

        // Set the attribute's Path property value, based on the queue's Path
        // property value.
        attribute.Path = queue.Path;

        // Display the new value of the attribute's Path property.
        Console.WriteLine("attribute.Path: {0}", attribute.Path);

    }

    public void PermissionAccessExample()
    {

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Create a new instance of MessageQueuePermissionAttribute.
        MessageQueuePermissionAttribute attribute =
            new MessageQueuePermissionAttribute(
            System.Security.Permissions.SecurityAction.Assert);

        // Set the attribute's PermissionAccess property value.
        attribute.PermissionAccess = MessageQueuePermissionAccess.Receive;

        // Display the new value of the attribute's PermissionAccess property.
        Console.WriteLine("attribute.PermissionAccess: {0}",
            attribute.PermissionAccess);

    }

    public void CreatePermissionExample()
    {

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Create a new instance of MessageQueuePermissionAttribute.
        MessageQueuePermissionAttribute attribute =
            new MessageQueuePermissionAttribute(
            System.Security.Permissions.SecurityAction.Assert);

        // Set the attribute's Path property value, based on the queue's Path
        // property value.
        attribute.Path = queue.Path;

        // Get an IPermission interface by calling the attribute's
        // CreatePermission() method.
        System.Security.IPermission permission = attribute.CreatePermission();

    }
}

Remarks

For more information about using attributes, see Attributes.

Constructors

MessageQueuePermissionAttribute(SecurityAction)

Initializes a new instance of the MessageQueuePermissionAttribute class.

Properties

Action

Gets or sets a security action.

(Inherited from SecurityAttribute)
Category

Gets or sets the queue category.

Label

Gets or sets the queue description.

MachineName

Gets or sets the name of the computer where the Message Queuing queue is located.

Path

Gets or sets the queue's path.

PermissionAccess

Gets or sets the permission access levels used in the permissions request.

TypeId

When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Unrestricted

Gets or sets a value indicating whether full (unrestricted) permission to the resource protected by the attribute is declared.

(Inherited from SecurityAttribute)

Methods

CreatePermission()

Creates the permission based on the requested access levels, category, label, computer name, and path that are set through the PermissionAccess, Category, Label, MachineName, and Path properties on the attribute.

Equals(Object)

Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode()

Returns the hash code for this instance.

(Inherited from Attribute)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object)

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to

See also