2
0

XmlSecureResolver.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. #if NET_1_0
  22. #endif
  23. #if NET_1_1
  24. //
  25. // System.Xml.XmlSecureResolver.cs
  26. //
  27. // Author: Atsushi Enomoto ([email protected])
  28. //
  29. // (C) 2003 Atsushi Enomoto
  30. //
  31. using System;
  32. using System.Net;
  33. using System.Security;
  34. using System.Security.Policy;
  35. using System.Security.Permissions;
  36. namespace System.Xml
  37. {
  38. public class XmlSecureResolver : XmlResolver
  39. {
  40. #region Static Members
  41. public static Evidence CreateEvidenceForUrl (string securityUrl)
  42. {
  43. Evidence e = new Evidence ();
  44. Url url = null;
  45. Zone zone = null;
  46. Site site = null;
  47. if (securityUrl != null) {
  48. try {
  49. if (securityUrl.Length > 0)
  50. url = new Url (securityUrl);
  51. } catch (ArgumentException) {
  52. }
  53. try {
  54. zone = Zone.CreateFromUrl (securityUrl);
  55. } catch (ArgumentException) {
  56. }
  57. try {
  58. if (securityUrl.Length > 0)
  59. site = Site.CreateFromUrl (securityUrl);
  60. } catch (ArgumentException) {
  61. }
  62. }
  63. if (url != null)
  64. e.AddHost (url);
  65. if (zone != null)
  66. e.AddHost (zone);
  67. if (site != null)
  68. e.AddHost (site);
  69. return e;
  70. }
  71. #endregion
  72. XmlResolver resolver;
  73. PermissionSet permissionSet;
  74. // Evidence evidence;
  75. #region .ctor and Finalizer
  76. public XmlSecureResolver (
  77. XmlResolver resolver, Evidence evidence)
  78. {
  79. this.resolver = resolver;
  80. // this.evidence = evidence;
  81. this.permissionSet = SecurityManager.ResolvePolicy (evidence);
  82. }
  83. public XmlSecureResolver (
  84. XmlResolver resolver, PermissionSet permissionSet)
  85. {
  86. this.resolver = resolver;
  87. this.permissionSet = permissionSet;
  88. }
  89. public XmlSecureResolver (
  90. XmlResolver resolver, string securityUrl)
  91. : this (resolver, CreateEvidenceForUrl (securityUrl))
  92. {
  93. }
  94. #endregion
  95. #region Property
  96. public override ICredentials Credentials {
  97. set { resolver.Credentials = value; }
  98. }
  99. #endregion
  100. #region Methods
  101. public override object GetEntity (
  102. Uri absoluteUri, string role, Type ofObjectToReturn)
  103. {
  104. permissionSet.PermitOnly ();
  105. return resolver.GetEntity (absoluteUri, role, ofObjectToReturn);
  106. }
  107. public override Uri ResolveUri (Uri baseUri, string relativeUri)
  108. {
  109. return resolver.ResolveUri (baseUri, relativeUri);
  110. }
  111. #endregion
  112. }
  113. }
  114. #endif