5

System.Messaging.MessageQueue 类不提供设置队列所有权的方法。如何以编程方式设置 MSMQ 消息队列的所有者?

4

1 回答 1

6

简短的回答是 p/invoke 对 windows api 函数的调用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();
}

可以在github上找到定义SetOwner扩展方法的完整类System.Messaging.MessageQueue

于 2012-06-30T04:35:43.850 回答