2012-06-19 70 views
2

作爲我正在開發的應用程序的一部分,我試圖利用Microsoft消息隊列功能,並且在權限設置方面遇到了問題。如何以編程方式設置專用MSMQ隊列上的權限設置以進行遠程訪問?

我想在服務器計算機上創建一個隊列並將消息發送給它,並通過本地網絡從客戶端計算機訪問它。兩臺電腦(都是Windows 7家庭高級版)都位於同一LAN網絡上,並且能夠互相ping通。我將知道創建的隊列的確切路徑,所以我猜私人隊列可以。

我將我的代碼基於this example並使用IronPython從System.Messaging命名空間訪問MessageQueue class。從該示例中的發送&接收安裝方法的效果來創建一個隊列,並且在一個控制檯向它發送一個消息:

>>> localQueue = MessageQueue.Create('.\\private$\\testQueue') 
>>> localQueue.FormatName 
'DIRECT=OS:<clientMachineName>\\private$\\testQueue' 
>>> localQueue.Send('hello from other console') 

,然後通過下面的代碼在它訪問隊列和PEEK在另一個控制檯:

>>> SemiRemoteQueue = MessageQueue('FormatName:Direct=OS:<clientMachineName>\\private$\\testQueue') 
>>> SemiRemoteQueue.Peek() 
<System.Messaging.Message object at 0x000000000000002F [System.Messaging.Message 
]> 

當我在服務器計算機上創建一個新隊列時,我似乎能夠從客戶端計算機建立連接,但無法從中獲取任何消息數據。從客戶端偷看隊列在服務器上創建,給出了下面的「訪問被拒絕」的錯誤消息:

>>> ReallyRemoteQueue = MessageQueue('FormatName:Direct=OS:<serverMachineName>\\private$\\remoteTestQueue') 
>>> ReallyRemoteQueue.Peek() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
EnvironmentError: System.Messaging.MessageQueueException (0x80004005): Access to 
Message Queuing system is denied. 
    at System.Messaging.MessageQueue.MQCacheableInfo.get_ReadHandle() 
    at System.Messaging.MessageQueue.StaleSafeReceiveMessage(UInt32 timeout, Int3 
2 action, MQPROPS properties, NativeOverlapped* overlapped, ReceiveCallback rece 
iveCallback, CursorHandle cursorHandle, IntPtr transaction) 
    at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 actio 
n, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction in 
ternalTransaction, MessageQueueTransactionType transactionType) 
    at System.Messaging.MessageQueue.Peek() 
    at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame 
frame) 
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
    at Microsoft.Scripting.Interpreter.LightLambda.Run3[T0,T1,T2,TRet](T0 arg0, T 
1 arg1, T2 arg2) 
    at IronPython.Compiler.Ast.CallExpression.Invoke0Instruction.Run(InterpretedF 
rame frame) 
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
    at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 a 
rg1) 
    at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 
    at IronPython.Compiler.PythonScriptCode.Run(Scope scope) 
    at IronPython.Hosting.PythonCommandLine.<>c__DisplayClass1.<RunOneInteraction 
>b__0() 

我發現,訪問遠程專用MSMQ隊列應該是可能的,但無法弄清楚如何設置正確的權限。我已經在防火牆設置中允許MSMQ,甚至在防火牆關閉的情況下嘗試過。找到了關於將權限設置應用到隊列文件的一些討論,但這沒有幫助,因爲我沒有安全設置爲他們工作的隊列文件。我並沒有試着按照建議給予完全訪問'匿名登錄'的選項,但最終想以編程方式(在代碼中)而不是在文件的上下文菜單中設置權限。我應該使用MessageQueing類中的SetPermissions方法(http://msdn.microsoft.com/zh-cn/library/dd48yz36(v=vs.80))嗎?我如何指定其參數?

感謝您的任何建議!

回答

1

您需要在第一個實例中爲「匿名登錄」授予從隊列接收消息的權限。

我使用此(C#)代碼,用於設置我發現here權限:

AccessControlList acl = new AccessControlList(); 
Trustee owner = new Trustee(WindowsIdentity.GetCurrent().Name, Environment.MachineName, TrusteeType.User); 
MessageQueueAccessControlEntry aceOwner = new MessageQueueAccessControlEntry(owner, MessageQueueAccessRights.FullControl); 
acl.Add(aceOwner); 
messageQueue.SetPermissions(acl); 
+0

感謝斯圖爾特,我想: - 更改個別權限設置(隊列文件 - >安全 - >高級 - >更改Permissions->添加)並添加完全控制「Everyone」和「匿名登錄」 - 完成相同的包含「lqs」文件夾 - 遵循你的食譜;我可以爲User類型的Trustee設置權限,但是當我嘗試將計算機名稱作爲Trustee構造函數的第一個和第二個參數時,SetPermissions()方法無法解析它。 我比較了設置權限前後的隊列文件和安全選項相同... 仍然從遠程計算機得到相同的錯誤。 – phillchill

+0

剛剛嘗試[另一種重載的setPermission方法](http://msdn.microsoft.com/en-US/library/x58dfx7z(v = vs80)),這確實會更改文件中的安全設置。我可以指定UserName和ComputerName,但無法獲得遠程ComputerName解析。我怎麼能指定這個?可能工作組安裝是一個問題? – phillchill

2

localQueue.SetPermissions( 「所有人」, MessageQueueAccessRights.FullControl,AccessControlEntryType.Allow);

「每個人」可以是任何域帳戶,例如「域\ MyUserName輸入」

相關問題