The Labs \ Source Viewer \ SSCLI \ System.Security.Policy \ Zone

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //
  16. // Zone.cs
  17. //
  18. // Zone is an IIdentity representing Internet/Intranet/MyComputer etc.
  19. //
  20. namespace System.Security.Policy
  21. {
  22.     using System.Security.Util;
  23.     using ZoneIdentityPermission = System.Security.Permissions.ZoneIdentityPermission;
  24.     using System.Runtime.CompilerServices;
  25.     using System.Runtime.Serialization;
  26.    
  27.     [Serializable()]
  28.     [System.Runtime.InteropServices.ComVisible(true)]
  29.     public sealed class Zone : IIdentityPermissionFactory, IBuiltInEvidence
  30.     {
  31.         [OptionalField(VersionAdded = 2)]
  32.         private string m_url;
  33.         private SecurityZone m_zone;
  34.        
  35.         private static readonly string[] s_names = {"MyComputer", "Intranet", "Trusted", "Internet", "Untrusted", "NoZone"};
  36.        
  37.         internal Zone()
  38.         {
  39.             m_url = null;
  40.             m_zone = SecurityZone.NoZone;
  41.         }
  42.        
  43.         public Zone(SecurityZone zone)
  44.         {
  45.             if (zone < SecurityZone.NoZone || zone > SecurityZone.Untrusted)
  46.                 throw new ArgumentException(Environment.GetResourceString("Argument_IllegalZone"));
  47.            
  48.             m_url = null;
  49.             m_zone = zone;
  50.         }
  51.        
  52.         private Zone(string url)
  53.         {
  54.             m_url = url;
  55.             m_zone = SecurityZone.NoZone;
  56.         }
  57.        
  58.         public static Zone CreateFromUrl(string url)
  59.         {
  60.             if (url == null)
  61.                 throw new ArgumentNullException("url");
  62.            
  63.             return new Zone(url);
  64.         }
  65.        
  66.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  67.         private static extern SecurityZone _CreateFromUrl(string url);
  68.        
  69.         public IPermission CreateIdentityPermission(Evidence evidence)
  70.         {
  71.             return new ZoneIdentityPermission(SecurityZone);
  72.         }
  73.        
  74.         public SecurityZone SecurityZone {
  75.             get {
  76.                 if (m_url != null)
  77.                     m_zone = _CreateFromUrl(m_url);
  78.                 return m_zone;
  79.             }
  80.         }
  81.        
  82.         public override bool Equals(object o)
  83.         {
  84.             if (o is Zone) {
  85.                 Zone z = (Zone)o;
  86.                 return SecurityZone == z.SecurityZone;
  87.             }
  88.             return false;
  89.         }
  90.        
  91.         public override int GetHashCode()
  92.         {
  93.             return (int)SecurityZone;
  94.         }
  95.        
  96.         public object Copy()
  97.         {
  98.             Zone z = new Zone();
  99.            
  100.             z.m_zone = m_zone;
  101.             z.m_url = m_url;
  102.            
  103.             return z;
  104.         }
  105.        
  106.         internal SecurityElement ToXml()
  107.         {
  108.             SecurityElement elem = new SecurityElement("System.Security.Policy.Zone");
  109.             // If you hit this assert then most likely you are trying to change the name of this class.
  110.             // This is ok as long as you change the hard coded string above and change the assert below.
  111.             BCLDebug.Assert(this.GetType().FullName.Equals("System.Security.Policy.Zone"), "Class name changed!");
  112.            
  113.             elem.AddAttribute("version", "1");
  114.             if (SecurityZone != SecurityZone.NoZone)
  115.                 elem.AddChild(new SecurityElement("Zone", s_names[(int)SecurityZone]));
  116.             else
  117.                 elem.AddChild(new SecurityElement("Zone", s_names[s_names.Length - 1]));
  118.             return elem;
  119.         }
  120.        
  121.         /// <internalonly/>
  122.         int IBuiltInEvidence.OutputToBuffer(char[] buffer, int position, bool verbose)
  123.         {
  124.             buffer[position] = BuiltInEvidenceHelper.idZone;
  125.             BuiltInEvidenceHelper.CopyIntToCharArray((int)SecurityZone, buffer, position + 1);
  126.             return position + 3;
  127.         }
  128.        
  129.         /// <internalonly/>
  130.         int IBuiltInEvidence.GetRequiredSize(bool verbose)
  131.         {
  132.             return 3;
  133.         }
  134.        
  135.         /// <internalonly/>
  136.         int IBuiltInEvidence.InitFromBuffer(char[] buffer, int position)
  137.         {
  138.             m_url = null;
  139.             m_zone = (SecurityZone)BuiltInEvidenceHelper.GetIntFromCharArray(buffer, position);
  140.             return position + 2;
  141.         }
  142.        
  143.         public override string ToString()
  144.         {
  145.             return ToXml().ToString();
  146.         }
  147.        
  148.         // INormalizeForIsolatedStorage is not implemented for startup perf
  149.         // equivalent to INormalizeForIsolatedStorage.Normalize()
  150.         internal object Normalize()
  151.         {
  152.             return s_names[(int)SecurityZone];
  153.         }
  154.     }
  155. }

Developer Fusion