我试图确保我的 ASP.Net 库能够在中等信任下工作。然而,我遇到了问题,因为如果代码在中等信任度下运行,我需要禁用一些代码。
如何从 C# 确定当前应用程序是否为中等信任?
具体来说,我正在尝试从 web.config 读取 customErrors 部分,但出现安全错误
本文介绍了一种确定信任级别的机制:
找出 ASP.NET 中当前的信任级别 http://blogs.msdn.com/b/dmitryr/archive/2007/01/23/finding-out-the-current-trust-level-in-asp-net.aspx
这是代码,以防链接失效:
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;
}
我刚刚在一个 ASP.NET MVC3 应用程序中测试了它,该应用程序运行在 Medium 上,然后运行在 Full trust 上,并且它按照它的说明进行操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)