WebPermissionAttribute.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // System.Net.WebPermissionAttribute.cs
  3. //
  4. // Author:
  5. // Andreas Nahr ([email protected])
  6. //
  7. // (C) 2003 Andreas Nahr
  8. //
  9. using System;
  10. using System.Security;
  11. using System.Security.Permissions;using System.Text.RegularExpressions;
  12. namespace System.Net
  13. {
  14. [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class
  15. | AttributeTargets.Struct | AttributeTargets.Constructor
  16. | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
  17. [Serializable]
  18. public sealed class WebPermissionAttribute : CodeAccessSecurityAttribute
  19. {
  20. // Fields
  21. object m_accept;
  22. object m_connect;
  23. // Constructors
  24. public WebPermissionAttribute (SecurityAction action) : base (action)
  25. {
  26. }
  27. // Properties
  28. public string Accept {
  29. get { return m_accept.ToString (); }
  30. set {
  31. if (m_accept != null)
  32. throw new ArgumentException ("The parameter 'Accept' can be set only once.");
  33. if (value == null)
  34. throw new ArgumentException ("The parameter 'Accept' cannot be null.");
  35. m_accept = value;
  36. }
  37. }
  38. public string AcceptPattern {
  39. get { return m_accept.ToString (); }
  40. set {
  41. if (m_accept != null)
  42. throw new ArgumentException ("The parameter 'Accept' can be set only once.");
  43. if (value == null)
  44. throw new ArgumentException ("The parameter 'Accept' cannot be null.");
  45. m_accept = new Regex (value, RegexOptions.IgnoreCase);
  46. }
  47. }
  48. public string Connect {
  49. get { return m_connect.ToString (); }
  50. set {
  51. if (m_connect != null)
  52. throw new ArgumentException ("The parameter 'Connect' can be set only once.");
  53. if (value == null)
  54. throw new ArgumentException ("The parameter 'Connect' cannot be null.");
  55. m_connect = value;
  56. }
  57. }
  58. public string ConnectPattern {
  59. get { return m_connect.ToString (); }
  60. set {
  61. if (m_connect != null)
  62. throw new ArgumentException ("The parameter 'Connect' can be set only once.");
  63. if (value == null)
  64. throw new ArgumentException ("The parameter 'Connect' cannot be null.");
  65. m_connect = new Regex (value, RegexOptions.IgnoreCase);
  66. }
  67. }
  68. // Methods
  69. public override IPermission CreatePermission ()
  70. {
  71. if (this.Unrestricted)
  72. return new WebPermission (PermissionState.Unrestricted);
  73. WebPermission newPermission = new WebPermission ();
  74. if (m_accept != null)
  75. {
  76. if (m_accept is Regex)
  77. newPermission.AddPermission (NetworkAccess.Accept, (Regex)m_accept);
  78. else
  79. newPermission.AddPermission (NetworkAccess.Accept, (string)m_accept);
  80. }
  81. if (m_connect != null)
  82. {
  83. if (m_connect is Regex)
  84. newPermission.AddPermission (NetworkAccess.Connect, (Regex)m_connect);
  85. else
  86. newPermission.AddPermission (NetworkAccess.Connect, (string)m_connect);
  87. }
  88. return newPermission;
  89. }
  90. }
  91. }