Ear*_*rlz 5 c# asp.net medium-trust
我正在努力确保我的ASP.Net库在Medium Trust下运行.我遇到了问题,因为如果它在中等信任下运行,我需要禁用一些代码.
如何从C#确定当前应用程序是否为中等信任?
具体来说,我正在尝试从web.config中读取customErrors部分,并且我遇到了安全性错误
本文介绍了一种确定信任级别的机制:
这是代码,以防链接失效:
AspNetHostingPermissionLevel GetCurrentTrustLevel() {
foreach (AspNetHostingPermissionLevel trustLevel in
new AspNetHostingPermissionLevel [] {
AspNetHostingPermissionLevel.Unrestricted,
AspNetHostingPermissionLevel.High,
AspNetHostingPermissionLevel.Medium,
AspNetHostingPermissionLevel.Low,
AspNetHostingPermissionLevel.Minimal
} ) {
try {
new AspNetHostingPermission(trustLevel).Demand();
}
catch (System.Security.SecurityException ) {
continue;
}
return trustLevel;
}
return AspNetHostingPermissionLevel.None;
}
Run Code Online (Sandbox Code Playgroud)
我刚刚在一个 ASP.NET MVC3 应用程序中测试了它,该应用程序运行在 Medium 上,然后运行在 Full trust 上,并且它按照它的说明进行操作。