简体   繁体   English

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

[英]Get current/active security zone of a .NET application?

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. 我找到了System.Security.SecurityZone枚举,但似乎无法找到任何会返回我正在运行的内容。

Does anyone have any tips? 有人有任何提示吗?

Basically I want to find out if my application is running in MyComputer, Intranet, Internet, Untrusted, Trusted, etc. 基本上我想知道我的应用程序是否在MyComputer,Intranet,Internet,Untrusted,Trusted等运行。


Edit: Here's the minor test-app I wrote to find this code, thanks to @blowdart . 编辑:这是我编写的用于查找此代码的次要测试应用程序,感谢@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;
                }
            }
        }
    }
}

You need to look at the CAS evidence for the current assembly; 您需要查看当前程序集的CAS证据;

this.GetType().Assembly.Evidence this.GetType()。Assembly.Evidence

Assembly.Evidence is a property Evidence object. Assembly.Evidence是一个属性Evidence对象。 From this you can enumerate the evidence and look for the zone which appears as a <System.Security.Policy.Zone> element. 从中可以枚举证据并查找显示为<System.Security.Policy.Zone>元素的区域。

In .NET 3.5 you can simplify the code with LINQ: 在.NET 3.5中,您可以使用LINQ简化代码:

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

From .NET 4.0 you have a convenient GetHostEvidence method: 从.NET 4.0开始,您可以使用方便的GetHostEvidence方法:

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

Note that from .NET 4.0 evidence classes derive from the EvidenceBase base class. 请注意,从.NET 4.0开始,证据类派生自EvidenceBase基类。

HTH, György HTH,György

You can also use 你也可以使用

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

instead of 代替

this.GetType().Assembly.Evidence

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM