获取 .NET 应用程序的当前/活动安全区域?
我有一个行为奇怪的应用程序,为了验证,我想查看它当前在哪个安全区域下运行。
我找到了 System.Security.SecurityZone 枚举,但似乎找不到任何可以返回我正在运行的内容的内容。
有人有什么建议吗?
基本上,我想查明我的应用程序是否在 MyComputer、Intranet、Internet、不受信任、受信任等中运行。
编辑: 这是我编写的用于查找此代码的小测试应用程序,感谢 @blowdart。
using System;
using System.Reflection;
namespace zone_check
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(".NET version: " + Environment.Version);
foreach (Object ev in Assembly.GetExecutingAssembly().Evidence)
{
if (ev is System.Security.Policy.Zone)
{
System.Security.Policy.Zone zone = (System.Security.Policy.Zone)ev;
Console.WriteLine("Security zone: " + zone.SecurityZone);
break;
}
}
}
}
}
I have an application that behaves oddly, and just to verify, I'd like to see which security zone it is currently running under.
I've found the System.Security.SecurityZone enum, but can't seem to find anything that will return which of these I'm running under.
Does anyone have any tips?
Basically I want to find out if my application is running in MyComputer, Intranet, Internet, Untrusted, Trusted, etc.
Edit: Here's the minor test-app I wrote to find this code, thanks to @blowdart.
using System;
using System.Reflection;
namespace zone_check
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(".NET version: " + Environment.Version);
foreach (Object ev in Assembly.GetExecutingAssembly().Evidence)
{
if (ev is System.Security.Policy.Zone)
{
System.Security.Policy.Zone zone = (System.Security.Policy.Zone)ev;
Console.WriteLine("Security zone: " + zone.SecurityZone);
break;
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要查看当前程序集的 CAS 证据;
this.GetType().Assembly.Evidence
Assembly.Evidence< /a> 是一个属性 Evidence 对象。 由此,您可以枚举证据并寻找显示为的区域 元素。
You need to look at the CAS evidence for the current assembly;
this.GetType().Assembly.Evidence
Assembly.Evidence is a property Evidence object. From this you can enumerate the evidence and look for the zone which appears as a <System.Security.Policy.Zone> element.
在 .NET 3.5 中,您可以使用 LINQ 简化代码:
从 .NET 4.0 开始,您有一个方便的
GetHostEvidence
方法:请注意,从 .NET 4.0 开始,证据类派生自
EvidenceBase
基班级。哈特哈,
吉尔吉
In .NET 3.5 you can simplify the code with LINQ:
From .NET 4.0 you have a convenient
GetHostEvidence
method:Note that from .NET 4.0 evidence classes derive from the
EvidenceBase
base class.HTH,
György
您也可以使用
代替
You can also use
instead of