2012-07-20 47 views
0

我想讓ASP.NET應用程序使用RabbitMQ的本機客戶端寫入消息隊列。爲此,我將RabbitMQ dll放在我的Web應用程序的/ bin目錄中,並創建了一個.aspx測試頁。使用共享配置時,IIS 7.5中的代碼訪問安全異常

當我測試腳本時,我得到以下異常System.Security.SecurityException:請求類型'System.Security.Permissions.EnvironmentPermission,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 '失敗。

進一步調查問題我相信這是由於dll物理存儲在另一臺計算機上的共享目錄引起的。研究導致我通過執行以下操作來調整代碼訪問安全策略:

c:\Windows\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -m -ag 1 -url "file://\\server\IISSharedContent\webfarm\stage\dev-stage\bin\*" FullTrust -exclusive on 

重新啓動IIS後,似乎無法工作。

我也試過:

  • 設置應用程序池標識爲網絡服務
  • 設置應用程序池「加載用戶配置文件」爲True
  • 添加到web.config

有沒有人有任何見解可以幫助我?


完整的堆棧跟蹤:

[SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.] 
     System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0 
     System.Security.CodeAccessPermission.Demand() +54 
     System.Environment.GetEnvironmentVariable(String variable) +135 
     RabbitMQ.Client.Protocols.ReadEnvironmentVariable() +22 
     RabbitMQ.Client.Protocols.FromEnvironment(String appSettingsKey) +37 
     RabbitMQ.Client.ConnectionFactory..ctor() +176 
     ASP.api_messagesearch_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in \\server\iissharedcontent\webfarm\stage\dev-stage\api\MessageSearch.aspx:9 
     System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +115 
     System.Web.UI.Page.Render(HtmlTextWriter writer) +38 
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11070247 
     System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11069786 
     System.Web.UI.Page.ProcessRequest() +91 
     System.Web.UI.Page.ProcessRequest(HttpContext context) +240 
     ASP.api_messagesearch_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\cb973a2c\79ccea1e\App_Web_x0q-uejx.0.cs:0 
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599 
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171 

代碼爲我的測試腳本:

ConnectionFactory rabbitConFactory = new ConnectionFactory(); 
rabbitConFactory.Address = "hostname"; 
rabbitConFactory.VirtualHost = "/path"; 
rabbitConFactory.UserName = "user"; 
rabbitConFactory.Password = "password"; 

IConnection connection = rabbitConFactory.CreateConnection(); 

IModel channel = connection.CreateModel(); 

Dictionary<String, Object> args = new Dictionary<string, object>(); 
args.Add("x-ha-policy", "all"); 
String ExchangeName = "SearchInstructions"; 
channel.ExchangeDeclare(ExchangeName, "topic", true, false, args); 

Guid myId = new Guid(); 
String RoutingKey = myId.ToString() + ".Instruction"; 
IBasicProperties MessageProperties = channel.CreateBasicProperties(); 
MessageProperties.SetPersistent(true); 
MessageProperties.ContentEncoding = "UTF-8"; 
MessageProperties.ContentType = "text/xml"; 
MessageProperties.Headers.Add("x-origin", 
Request.ServerVariables["ServerName"]); 

String messageContent = "<foo />"; 
byte[] data = System.Text.Encoding.UTF8.GetBytes(messageContent); 
channel.BasicPublish(ExchangeName,RoutingKey, MessageProperties, data); 

channel.Close(); 
connection.Close(); 

回答

0

奧凱已經竭盡全力來應付這一問題進行兩小時的大部分時間我想出了一個簡單明瞭的解決方案:

同時使用32位和64位版本的caspol.exe

爲了正確設置這個共享的信任,我不得不運行兩次caspol.exe。

c:\Windows\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -m -ag 1 -url "file://\\server\IISSharedContent\webfarm\stage\dev-stage\bin\*" FullTrust 

,然後運行

c:\Windows\Microsoft.NET\Framework64\v2.0.50727\CasPol.exe -m -ag 1 -url "file://\\server\IISSharedContent\webfarm\stage\dev-stage\bin\*" FullTrust 

我重新啓動IIS和LO,一切工作就像一個冠軍......(當然除了一些其他錯誤)。