Reputation: 8724
How can a .NET application detect the trust level in which it is running under?
Specifically, I'm wanting a way to do something like
if (RUNNING IN GREATER THAN MEDIUM TRUST) {
// set private fields & properties using reflection
}
My current solution is to use
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;
}
}
}
But, it's a bit of a hack.
Upvotes: 8
Views: 2173
Reputation: 310977
From .NET 4.0 onwards there is the AppDomain.IsFullyTrusted
property which may be helpful.
If you wish to test this on the current app domain, use:
if (AppDomain.CurrentDomain.IsFullyTrusted)
{
// ...
}
Upvotes: 6
Reputation: 8724
public static class CodeAccessSecurity {
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 __IOS__
return false;
#elif __ANDROID__
return false;
#else
if (!_determinedUnrestrictedFeatureSet)
lock (_threadLock) {
if (!_determinedUnrestrictedFeatureSet) {
_unrestrictedFeatureSet = AppDomain.CurrentDomain.ApplicationTrust == null || AppDomain.CurrentDomain.ApplicationTrust.DefaultGrantSet.PermissionSet.IsUnrestricted();
_determinedUnrestrictedFeatureSet = true;
}
}
return _unrestrictedFeatureSet;
#endif
}
}
}
}
Upvotes: -1
Reputation: 32561
Maybe this is helpful:
ActivationContext ac = AppDomain.CurrentDomain.ActivationContext;
ApplicationIdentity ai = ac.Identity;
var applicationTrust = new System.Security.Policy.ApplicationTrust(ai);
var isUnrestricted = applicationTrust.DefaultGrantSet.PermissionSet.IsUnrestricted();
Or
AppDomain.CurrentDomain.ApplicationTrust
.DefaultGrantSet.PermissionSet.IsUnrestricted();
Upvotes: 7
Reputation: 6735
You can use this code:
private AspNetHostingPermissionLevel[] aspNetHostingPermissionLevel = new AspNetHostingPermissionLevel[]
{
AspNetHostingPermissionLevel.Unrestricted,
AspNetHostingPermissionLevel.High,
AspNetHostingPermissionLevel.Medium,
AspNetHostingPermissionLevel.Low,
AspNetHostingPermissionLevel.Minimal
};
public AspNetHostingPermissionLevel GetTrustLevel()
{
foreach (AspNetHostingPermissionLevel aspNetHostingPermissionLevel in aspNetHostingPermissionLevel)
{
try
{
new AspNetHostingPermission(aspNetHostingPermissionLevel).Demand();
}
catch (System.Security.SecurityException)
{
continue;
}
return aspNetHostingPermissionLevel;
}
return AspNetHostingPermissionLevel.None;
}
Upvotes: 1