简体   繁体   English

在C#中创建新的消息队列(MSMQ)时设置默认权限

[英]Set default permissions when creating a new Message Queue (MSMQ) in C#

I am trying to programmatically create several Message Queues on a Windows 2003x64/2008R2 Server. 我试图以编程方式在Windows 2003x64 / 2008R2服务器上创建多个Message Queues When a Queue is created, Windows automatically applies default permissions to the Queue. 创建队列时,Windows会自动将默认权限应用于队列。

For MSMQ 4, the following users are added by default 对于MSMQ 4,默认情况下会添加以下用户

  • Everyone 大家
  • Creator of the queue 队列的创建者
  • ANONYMOUS LOGIN 匿名登录

When I set the permissions for a queue using MessageQueue.SetPermissions() , the specified AccessControlList is only appended to the default security permissions. 当我使用MessageQueue.SetPermissions()设置队列的权限时,指定的AccessControlList仅附加到默认安全权限。

Is there any way the default permissions can be removed or overwritten? 是否有任何方法可以删除或覆盖默认权限? In this MSDN Article at the bottom, it states that 这篇MSDN文章的底部,它说明了这一点

You can't, though, customise the defaults as they are hard-coded. 但是,您不能自定义默认值,因为它们是硬编码的。

I know that the settings of a queue are persisted in a file, located in C:\\Windows\\System32\\msmq\\storage\\lqs . 我知道队列的设置保存在一个文件中,位于C:\\Windows\\System32\\msmq\\storage\\lqs In this file there's a Security property that represents the permissions for the queue. 在此文件中有一个Security属性,表示队列的权限。 Might editing this key be an option? 可以选择编辑此密钥吗? Doing this, however, seems kind of odd to me. 但是,这样做对我来说似乎很奇怪。

I'm looking for a proper way to specify my own AccessControlList that overwrites the default security permissions of a queue. 我正在寻找一种正确的方法来指定我自己的AccessControlList ,它会覆盖队列的默认安全权限。 Either when it's being created, or afterwards. 无论是在创建时还是之后。

Any help is appreciated, 任何帮助表示赞赏,

Thanks. 谢谢。

If you can't remove or revoke permissions to those default groups you can always try to deny rights to them. 如果您无法删除或撤消对这些默认组的权限,则始终可以尝试拒绝对这些组的权限。 Deny takes precedence over allow. 拒绝优先于允许。 This code works: 此代码有效:

MessageQueue queue = new MessageQueue(".\\Private$\\QueueName");
queue.SetPermissions("Everyone", MessageQueueAccessRights.ReceiveMessage,
        AccessControlEntryType.Deny);

Revoking permissions ( AccessControlEntryType.Revoke ) should also work though. 撤销权限( AccessControlEntryType.Revoke )也应该有效。 Maybe there is an error in your code. 也许您的代码中存在错误。 Works on my machine. 适用于我的机器。

The article says: 文章说:

You can't, though, customise the defaults as they are hard-coded. 但是,您不能自定义默认值,因为它们是硬编码的。

and it means that you can't change what rights are given during queue creation but you can change them afterwards. 并且这意味着您无法更改队列创建期间给出的权限,但您可以在之后更改它们。

Edit: To get "Everyone" independent of OS-language: How to get the IdentityReference for "Everyone" to create MutexAccessRule on localized systems? 编辑:让“Everyone”独立于OS语言: 如何让“Everyone”的IdentityReference在本地化系统上创建MutexAccessRule?

I had the same exact issue with the ACL. 我和ACL有同样的问题。 When I switched to the SetPermissions() method things ran much better. 当我切换到SetPermissions()方法时,事情运行得更好。

The code below works for me: 以下代码适用于我:

                queue.SetPermissions(
                    "Everyone",
                    MessageQueueAccessRights.FullControl,
                    AccessControlEntryType.Allow);
                queue.SetPermissions(
                    "ANONYMOUS LOGON",
                    MessageQueueAccessRights.FullControl,
                    AccessControlEntryType.Allow);

Although Mike's answer is correct, it assumes the server has English as the language. 虽然Mike的答案是正确的,但它假设服务器使用英语作为语言。 If you use this code on a server which uses a different language (in this case Dutch) ... 如果您在使用不同语言的服务器上使用此代码(在本例中为荷兰语)...

 queue.SetPermissions( 
                     "Everyone",
                     MessageQueueAccessRights.FullControl,
                      AccessControlEntryType.Allow);

... you get the following exception: ...你得到以下例外:

Kan de naam Everyone niet omzetten (fout = 1332 ). Kan de naam Everyone niet omzetten(fout = 1332)。 System.Messaging.AccessControlList.MakeAcl(IntPtr oldAcl) bij System.Messaging.MessageQueue.SetPermissions(AccessControlList dacl) bij System.Messaging.MessageQueue.SetPermissions(String user, MessageQueueAccessRights rights, AccessControlEntryType entryType) System.Messaging.AccessControlList.MakeAcl(IntPtr oldAcl)bij System.Messaging.MessageQueue.SetPermissions(AccessControlList dacl)bij System.Messaging.MessageQueue.SetPermissions(String user,MessageQueueAccessRights rights,AccessControlEntryType entryType)

which roughly translates to 'Cannot convert name 'Everyone'. 大致翻译为'无法转换名称'每个人'。 Instead if you use this code you will get a localized version of 'Everyone': 相反,如果您使用此代码,您将获得“Everyone”的本地化版本:

using System.Security.Principal;

** code ommitted**

string everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null).Translate(typeof(NTAccount)).Value;
queue.SetPermissions( 
                   everyone,
                   MessageQueueAccessRights.FullControl,
                    AccessControlEntryType.Allow);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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