获取 .NET 应用程序的当前/活动安全区域?

发布于 2024-07-08 03:14:22 字数 979 浏览 5 评论 0原文

我有一个行为奇怪的应用程序,为了验证,我想查看它当前在哪个安全区域下运行。

我找到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

π浅易 2024-07-15 03:14:22

您需要查看当前程序集的 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.

脸赞 2024-07-15 03:14:22

在 .NET 3.5 中,您可以使用 LINQ 简化代码:

Zone z = a.Evidence.OfType<Zone>().First();

从 .NET 4.0 开始,您有一个方便的 GetHostEvidence 方法:

Zone z = Assembly.GetExecutingAssembly().Evidence.GetHostEvidence<Zone>();

请注意,从 .NET 4.0 开始,证据类派生自 EvidenceBase 基班级。

哈特哈,
吉尔吉

In .NET 3.5 you can simplify the code with LINQ:

Zone z = a.Evidence.OfType<Zone>().First();

From .NET 4.0 you have a convenient GetHostEvidence method:

Zone z = Assembly.GetExecutingAssembly().Evidence.GetHostEvidence<Zone>();

Note that from .NET 4.0 evidence classes derive from the EvidenceBase base class.

HTH,
György

奢望 2024-07-15 03:14:22

您也可以使用

Evidence e = Thread.CurrentThread.GetType().Assembly.Evidence;

代替

this.GetType().Assembly.Evidence

You can also use

Evidence e = Thread.CurrentThread.GetType().Assembly.Evidence;

instead of

this.GetType().Assembly.Evidence
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文