如何设置消息队列的所有者?

时间:2012-06-30 04:35:43

标签: c# msmq

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

1 个答案:

答案 0 :(得分:4)

简短的回答是p /调用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();
}

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

相关问题