WebPermissionAttribute.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // System.Net.WebPermissionAttribute.cs
  3. //
  4. // Authors:
  5. // Andreas Nahr ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Andreas Nahr
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Security;
  31. using System.Security.Permissions;
  32. namespace System.Net {
  33. [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class
  34. | AttributeTargets.Struct | AttributeTargets.Constructor
  35. | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
  36. [Serializable]
  37. public sealed class WebPermissionAttribute : CodeAccessSecurityAttribute {
  38. // Fields
  39. object m_accept;
  40. object m_connect;
  41. // Constructors
  42. public WebPermissionAttribute (SecurityAction action)
  43. : base (action)
  44. {
  45. }
  46. // Properties
  47. public string Accept {
  48. get {
  49. if (m_accept == null)
  50. return null;
  51. return (m_accept as WebPermissionInfo).Info;
  52. }
  53. set {
  54. if (m_accept != null)
  55. AlreadySet ("Accept", "Accept");
  56. m_accept = new WebPermissionInfo (WebPermissionInfoType.InfoString, value);
  57. }
  58. }
  59. public string AcceptPattern {
  60. get {
  61. if (m_accept == null)
  62. return null;
  63. return (m_accept as WebPermissionInfo).Info;
  64. }
  65. set {
  66. if (m_accept != null)
  67. AlreadySet ("Accept", "AcceptPattern");
  68. if (value == null)
  69. throw new ArgumentNullException ("AcceptPattern");
  70. m_accept = new WebPermissionInfo (WebPermissionInfoType.InfoUnexecutedRegex , value);
  71. }
  72. }
  73. public string Connect {
  74. get {
  75. if (m_connect == null)
  76. return null;
  77. return (m_connect as WebPermissionInfo).Info;
  78. }
  79. set {
  80. if (m_connect != null)
  81. AlreadySet ("Connect", "Connect");
  82. m_connect = new WebPermissionInfo (WebPermissionInfoType.InfoString, value);
  83. }
  84. }
  85. public string ConnectPattern {
  86. get {
  87. if (m_connect == null)
  88. return null;
  89. return (m_connect as WebPermissionInfo).Info;
  90. }
  91. set {
  92. if (m_connect != null)
  93. AlreadySet ("Connect", "ConnectConnectPattern");
  94. if (value == null)
  95. throw new ArgumentNullException ("ConnectPattern");
  96. m_connect = new WebPermissionInfo (WebPermissionInfoType.InfoUnexecutedRegex , value);
  97. }
  98. }
  99. // Methods
  100. public override IPermission CreatePermission ()
  101. {
  102. if (this.Unrestricted)
  103. return new WebPermission (PermissionState.Unrestricted);
  104. WebPermission newPermission = new WebPermission ();
  105. if (m_accept != null) {
  106. newPermission.AddPermission (NetworkAccess.Accept, (WebPermissionInfo) m_accept);
  107. }
  108. if (m_connect != null) {
  109. newPermission.AddPermission (NetworkAccess.Connect, (WebPermissionInfo) m_connect);
  110. }
  111. return newPermission;
  112. }
  113. // helpers
  114. internal void AlreadySet (string parameter, string property)
  115. {
  116. string msg = Locale.GetText ("The parameter '{0}' can be set only once.");
  117. throw new ArgumentException (String.Format (msg, parameter), property);
  118. }
  119. }
  120. }