2009-05-25 56 views
9

我正在用戶Guest下測試我的應用程序。它崩潰,出現以下錯誤。'UnauthorizedAccessException' - 'Global .net clr networking'

「UnauthorizedAccessException」 - 「Global.net CLR聯網」

現在,我知道我可以在機器上修改安全策略允許CLR代碼在客戶操作系統中運行是可信的,但什麼人應該在做商業應用?

(簽名,並添加CAS屬性?) 我目前正在閱讀整個安全部分,但我在時間捏,所以任何指針在正確的方向將不勝感激。

編輯:我已經追溯到使用Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase類的問題。如果包含此信息,則會顯示錯誤。 我在找東西添加到清單或其他方式,以便當應用程序安裝/運行時,它會要求適當的權限。我不想讓用戶親自致電caspol或其他工具。

環境細節: - 應用程序是使用.NET 3.0 - 操作系統是Vista的

下面是那些進入這些東西相關的堆棧跟蹤:

Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'Glo 
bal\.net clr networking' is denied. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.Threading.Mutex.<>c__DisplayClass3.<.ctor>b__0(Object userData) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCl 
eanup(TryCode code, CleanupCode backoutCode, Object userData) 
    at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& 
createdNew, MutexSecurity mutexSecurity) 
    at System.Diagnostics.SharedUtils.EnterMutexWithoutGlobal(String mutexName, M 
utex& mutex) 
    at System.Diagnostics.SharedPerformanceCounter.Verify(CategoryEntry* currentC 
ategoryPointer) 
    at System.Diagnostics.SharedPerformanceCounter.FindCategory(CategoryEntry** r 
eturnCategoryPointerReference) 
    at System.Diagnostics.SharedPerformanceCounter.GetCounter(String counterName, 
String instanceName, Boolean enableReuse, PerformanceCounterInstanceLifetime li 
fetime) 
    at System.Diagnostics.SharedPerformanceCounter..ctor(String catName, String c 
ounterName, String instanceName, PerformanceCounterInstanceLifetime lifetime) 
    at System.Diagnostics.PerformanceCounter.Initialize() 
    at System.Diagnostics.PerformanceCounter.set_RawValue(Int64 value) 
    at System.Net.NetworkingPerfCounters.Initialize() 
    at System.Net.Configuration.SettingsSectionInternal..ctor(SettingsSection sec 
tion) 
    at System.Net.Configuration.SettingsSectionInternal.get_Section() 
    at System.Net.Sockets.Socket.InitializeSockets() 
    at System.Net.Sockets.Socket.get_SupportsIPv4() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.get_ 
HostName() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Regi 
sterChannel(Boolean SecureChannel) 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(
String[] commandLine) 
+0

它在哪裏崩潰?在啓動?在正常操作中? – blowdart 2009-05-25 05:00:10

+0

在啓動過程中,我的代碼的任何部分被執行之前。 – moogs 2009-05-25 05:14:35

回答

5

是否有可能將此添加到您的app.config文件?

<configuration> 
    <system.net> 
     <settings> 
     <performanceCounters enabled="false" /> 
     </settings> 
    </system.net> 
</configuration> 

這將指示網絡類不嘗試創建在Guest帳戶下不起作用的性能計數器。

上述設置適用於.NET Framework 4及更高版本,但是由於早期版本中的錯誤而失敗。

1

從我明白了什麼,這是因爲來賓帳戶有一些非常奇怪的權限分配給它。這是而不是一個錯誤,說你不能使用網絡 - 基本的網絡仍然是部分信任。

發生此錯誤是因爲CLR無法訪問其在網絡操作期間使用的其自身的性能計數器之一。這個問題不應該發生在其他用戶帳戶中 - 您是否特別需要使用Guest?正常的有限用戶帳戶應該可以正常工作。客戶帳戶已知存在許多與.NET相關的訪問權限問題 - 該帳戶在實踐中很少使用,並且很少有事情經過測試。

關於代碼訪問安全性,默認情況下,您在哪個用戶上運行代碼並不重要 - CAS權限對所有用戶都是相同的。信任級別取決於可執行文件的位置 - 運行安裝在本地計算機上的某些內容可授予它完全信任,從其他位置運行授予部分信任(請參閱.NET Framework配置中的代碼組)。

+0

感謝您的回答。我需要我的應用程序在Guest帳戶下運行,沒有任何問題。 – moogs 2009-05-25 05:14:20

0

你可以命名您的組件,然後通過CASPOL給予他們充分的信任,但要儘量強使用

caspol -fulltrust assemblyName 

如桑德說Guest帳戶是不是一個真正的帳戶,這是不是一個正常登錄,並對它有非常嚴格的限制。

2

對於像我這樣的需要在框架2.x程序中支持來賓帳戶的窮人(即使安裝了CLR 4,一些舊的CLR2編譯程序仍然可以在CLR2下運行),這是一個hacky函數這將禁用此性能計數器初始化問題(請參閱Matt Ellis的答案。他的回答的問題是 - 正如其他人所述 - 它並不總是有效):

public static bool DisablePerfCountersIfNeeded() 
    { 
     try 
     { 
      NetworkInterface.GetIsNetworkAvailable(); 
      return false; 
     } 
     catch(UnauthorizedAccessException) 
     { 
     } 

     Type type = typeof(Uri).Assembly.GetType("System.Net.NetworkingPerfCounters"); 
     FieldInfo fi = type.GetField("initialized", BindingFlags.Static | BindingFlags.NonPublic); 
     bool initialized = (bool)fi.GetValue(null); 
     fi.SetValue(null, true); 
     return true; 
    }