简体   繁体   中英

How do I set the owner of a message queue?

The System.Messaging.MessageQueue class does not provide a way to set ownership of queue. How do I programmatically set the owner of a MSMQ message queue?

The short answer is to p/invoke a call to the windows api function MQSetQueueSecurity

void SetOwner(MessageQueue queue, byte[] sid, bool ownerDefaulted = false)
{
    var securityDescriptor = new Win32.SECURITY_DESCRIPTOR();
    if (!Win32.InitializeSecurityDescriptor(securityDescriptor, Win32.SECURITY_DESCRIPTOR_REVISION))
        throw new Win32Exception();

    if (!Win32.SetSecurityDescriptorOwner(securityDescriptor, sid, ownerDefaulted))
        throw new Win32Exception();

    if (Win32.MQSetQueueSecurity(queue.FormatName, Win32.OWNER_SECURITY_INFORMATION, securityDescriptor))
        throw new Win32Exception();
}

A complete class which defines a SetOwner extension method on System.Messaging.MessageQueue can be found on github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM