3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // UrlIdentityPermission.cs
8 // <OWNER>[....]</OWNER>
11 namespace System.Security.Permissions
14 #if FEATURE_CAS_POLICY
15 using SecurityElement = System.Security.SecurityElement;
16 #endif // FEATURE_CAS_POLICY
17 using System.Security.Util;
20 using System.Collections;
21 using System.Collections.Generic;
22 using System.Globalization;
23 using System.Runtime.Serialization;
24 using System.Diagnostics.Contracts;
26 [System.Runtime.InteropServices.ComVisible(true)]
27 [Serializable] sealed public class UrlIdentityPermission : CodeAccessPermission, IBuiltInPermission
29 //------------------------------------------------------
33 //------------------------------------------------------
35 [OptionalField(VersionAdded = 2)]
36 private bool m_unrestricted;
37 [OptionalField(VersionAdded = 2)]
38 private URLString[] m_urls;
41 // This field will be populated only for non X-AD scenarios where we create a XML-ised string of the Permission
42 [OptionalField(VersionAdded = 2)]
43 private String m_serializedPermission;
45 // This field is legacy info from v1.x and is never used in v2.0 and beyond: purely for serialization purposes
46 private URLString m_url;
49 private void OnDeserialized(StreamingContext ctx)
51 // v2.0 and beyond XML case
52 if (m_serializedPermission != null)
54 FromXml(SecurityElement.FromString(m_serializedPermission));
55 m_serializedPermission = null;
57 else if (m_url != null) //v1.x case where we read the m_site value
59 m_unrestricted = false;
60 m_urls = new URLString[1];
68 private void OnSerializing(StreamingContext ctx)
71 if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
73 m_serializedPermission = ToXml().ToString(); //for the v2 and beyond case
74 if (m_urls != null && m_urls.Length == 1) // for the v1.x case
80 private void OnSerialized(StreamingContext ctx)
82 if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
84 m_serializedPermission = null;
88 #endif // FEATURE_REMOTING
90 //------------------------------------------------------
92 // PUBLIC CONSTRUCTORS
94 //------------------------------------------------------
97 public UrlIdentityPermission(PermissionState state)
99 if (state == PermissionState.Unrestricted)
101 m_unrestricted = true;
103 else if (state == PermissionState.None)
105 m_unrestricted = false;
109 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
113 public UrlIdentityPermission( String site )
116 throw new ArgumentNullException( "site" );
117 Contract.EndContractBlock();
121 internal UrlIdentityPermission( URLString site )
123 m_unrestricted = false;
124 m_urls = new URLString[1];
128 // Internal function to append all the urls in m_urls to the input originList
129 internal void AppendOrigin(ArrayList originList)
136 for(n = 0; n < this.m_urls.Length; n++)
137 originList.Add(m_urls[n].ToString());
141 //------------------------------------------------------
143 // PUBLIC ACCESSOR METHODS
145 //------------------------------------------------------
151 m_unrestricted = false;
152 if(value == null || value.Length == 0)
156 m_urls = new URLString[1];
157 m_urls[0] = new URLString( value );
165 if(m_urls.Length == 1)
166 return m_urls[0].ToString();
167 throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
171 //------------------------------------------------------
173 // PRIVATE AND PROTECTED HELPERS FOR ACCESSORS AND CONSTRUCTORS
175 //------------------------------------------------------
177 //------------------------------------------------------
179 // CODEACCESSPERMISSION IMPLEMENTATION
181 //------------------------------------------------------
183 //------------------------------------------------------
185 // IPERMISSION IMPLEMENTATION
187 //------------------------------------------------------
190 public override IPermission Copy()
192 UrlIdentityPermission perm = new UrlIdentityPermission( PermissionState.None );
193 perm.m_unrestricted = this.m_unrestricted;
194 if (this.m_urls != null)
196 perm.m_urls = new URLString[this.m_urls.Length];
198 for(n = 0; n < this.m_urls.Length; n++)
199 perm.m_urls[n] = (URLString)this.m_urls[n].Copy();
204 public override bool IsSubsetOf(IPermission target)
212 if(m_urls.Length == 0)
216 UrlIdentityPermission that = target as UrlIdentityPermission;
218 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
219 if(that.m_unrestricted)
223 if(this.m_urls != null)
225 foreach(URLString usThis in this.m_urls)
228 if(that.m_urls != null)
230 foreach(URLString usThat in that.m_urls)
232 if(usThis.IsSubsetOf(usThat))
246 public override IPermission Intersect(IPermission target)
250 UrlIdentityPermission that = target as UrlIdentityPermission;
252 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
253 if(this.m_unrestricted && that.m_unrestricted)
255 UrlIdentityPermission res = new UrlIdentityPermission(PermissionState.None);
256 res.m_unrestricted = true;
259 if(this.m_unrestricted)
261 if(that.m_unrestricted)
263 if(this.m_urls == null || that.m_urls == null || this.m_urls.Length == 0 || that.m_urls.Length == 0)
265 List<URLString> alUrls = new List<URLString>();
266 foreach(URLString usThis in this.m_urls)
268 foreach(URLString usThat in that.m_urls)
270 URLString usInt = (URLString)usThis.Intersect(usThat);
275 if(alUrls.Count == 0)
277 UrlIdentityPermission result = new UrlIdentityPermission(PermissionState.None);
278 result.m_urls = alUrls.ToArray();
282 public override IPermission Union(IPermission target)
286 if((this.m_urls == null || this.m_urls.Length == 0) && !this.m_unrestricted)
290 UrlIdentityPermission that = target as UrlIdentityPermission;
292 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
293 if(this.m_unrestricted || that.m_unrestricted)
295 UrlIdentityPermission res = new UrlIdentityPermission(PermissionState.None);
296 res.m_unrestricted = true;
299 if (this.m_urls == null || this.m_urls.Length == 0)
301 if(that.m_urls == null || that.m_urls.Length == 0)
305 if(that.m_urls == null || that.m_urls.Length == 0)
307 List<URLString> alUrls = new List<URLString>();
308 foreach(URLString usThis in this.m_urls)
310 foreach(URLString usThat in that.m_urls)
313 foreach(URLString us in alUrls)
315 if(usThat.Equals(us))
324 UrlIdentityPermission result = new UrlIdentityPermission(PermissionState.None);
325 result.m_urls = alUrls.ToArray();
329 #if FEATURE_CAS_POLICY
330 public override void FromXml(SecurityElement esd)
332 m_unrestricted = false;
334 CodeAccessPermission.ValidateElement( esd, this );
335 String unr = esd.Attribute( "Unrestricted" );
336 if(unr != null && String.Compare(unr, "true", StringComparison.OrdinalIgnoreCase) == 0)
338 m_unrestricted = true;
341 String elem = esd.Attribute( "Url" );
342 List<URLString> al = new List<URLString>();
344 al.Add(new URLString( elem, true ));
345 ArrayList alChildren = esd.Children;
346 if(alChildren != null)
348 foreach(SecurityElement child in alChildren)
350 elem = child.Attribute( "Url" );
352 al.Add(new URLString( elem, true ));
356 m_urls = al.ToArray();
359 public override SecurityElement ToXml()
361 SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.UrlIdentityPermission" );
363 esd.AddAttribute( "Unrestricted", "true" );
364 else if (m_urls != null)
366 if (m_urls.Length == 1)
367 esd.AddAttribute( "Url", m_urls[0].ToString() );
371 for(n = 0; n < m_urls.Length; n++)
373 SecurityElement child = new SecurityElement("Url");
374 child.AddAttribute( "Url", m_urls[n].ToString() );
381 #endif // FEATURE_CAS_POLICY
384 int IBuiltInPermission.GetTokenIndex()
386 return UrlIdentityPermission.GetTokenIndex();
389 internal static int GetTokenIndex()
391 return BuiltInPermissionIndex.UrlIdentityPermissionIndex;