.NET應用程序如何檢測其運行所在的信任級別?檢測信任級別
具體來說,我想辦法做到像
if (RUNNING IN GREATER THAN MEDIUM TRUST) {
// set private fields & properties using reflection
}
我目前的解決方案是使用
public static class CodeAccessSecurityTool {
private static volatile bool _unrestrictedFeatureSet = false;
private static volatile bool _determinedUnrestrictedFeatureSet = false;
private static readonly object _threadLock = new object();
public static bool HasUnrestrictedFeatureSet {
get {
if (!_determinedUnrestrictedFeatureSet)
lock (_threadLock) {
if (!_determinedUnrestrictedFeatureSet) {
try {
// See if we're running in full trust
new PermissionSet(PermissionState.Unrestricted).Demand();
_unrestrictedFeatureSet = true;
} catch (SecurityException) {
_unrestrictedFeatureSet = false;
}
_determinedUnrestrictedFeatureSet = true;
}
}
return _unrestrictedFeatureSet;
}
}
}
但是,這是一個黑客攻擊的一位。
[這裏](http://stackoverflow.com/a/11660205/969613)是有人檢查,看看它們是否以管理員身份運行可能會很有用! – JMK 2013-02-21 12:04:51
@JMK:操作系統權限和CLR權限未連接(例如,所有用戶的Silverlight代碼都將以中等信任運行)。 – 2013-02-21 12:09:25
好的,我不知道,道歉! – JMK 2013-02-21 12:09:57