gpt4 book ai didi

.net - 检测信任级别

转载 作者:行者123 更新时间:2023-12-01 08:11:53 25 4
gpt4 key购买 nike

.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;
}
}
}

但是,这有点黑客。

最佳答案

也许这有帮助:

ActivationContext ac = AppDomain.CurrentDomain.ActivationContext;
ApplicationIdentity ai = ac.Identity;
var applicationTrust = new System.Security.Policy.ApplicationTrust(ai);
var isUnrestricted = applicationTrust.DefaultGrantSet.PermissionSet.IsUnrestricted();

或者
AppDomain.CurrentDomain.ApplicationTrust
.DefaultGrantSet.PermissionSet.IsUnrestricted();

关于.net - 检测信任级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15001626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com