How do I set the owner of a message queue?

房东的猫 提交于 2019-12-10 17:38:38

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/11271429/how-do-i-set-the-owner-of-a-message-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!