AuthenticationManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // System.Net.AuthenticationManager.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  9. //
  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.Collections;
  31. using System.Configuration;
  32. namespace System.Net
  33. {
  34. public class AuthenticationManager
  35. {
  36. static ArrayList modules;
  37. static object locker = new object ();
  38. private AuthenticationManager ()
  39. {
  40. }
  41. static void EnsureModules ()
  42. {
  43. lock (locker) {
  44. if (modules != null)
  45. return;
  46. modules = new ArrayList ();
  47. ConfigurationSettings.GetConfig ("system.net/authenticationModules");
  48. }
  49. }
  50. public static IEnumerator RegisteredModules {
  51. get {
  52. EnsureModules ();
  53. return modules.GetEnumerator ();
  54. }
  55. }
  56. internal static void Clear ()
  57. {
  58. EnsureModules ();
  59. lock (modules)
  60. modules.Clear ();
  61. }
  62. public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
  63. {
  64. if (request == null)
  65. throw new ArgumentNullException ("request");
  66. if (credentials == null)
  67. throw new ArgumentNullException ("credentials");
  68. if (challenge == null)
  69. throw new ArgumentNullException ("challenge");
  70. return DoAuthenticate (challenge, request, credentials);
  71. }
  72. static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
  73. {
  74. EnsureModules ();
  75. lock (modules) {
  76. foreach (IAuthenticationModule mod in modules) {
  77. Authorization auth = mod.Authenticate (challenge, request, credentials);
  78. if (auth == null)
  79. continue;
  80. auth.Module = mod;
  81. return auth;
  82. }
  83. }
  84. return null;
  85. }
  86. public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
  87. {
  88. if (request == null)
  89. throw new ArgumentNullException ("request");
  90. if (credentials == null)
  91. return null;
  92. EnsureModules ();
  93. lock (modules) {
  94. foreach (IAuthenticationModule mod in modules) {
  95. Authorization auth = mod.PreAuthenticate (request, credentials);
  96. if (auth == null)
  97. continue;
  98. auth.Module = mod;
  99. return auth;
  100. }
  101. }
  102. return null;
  103. }
  104. public static void Register (IAuthenticationModule authenticationModule)
  105. {
  106. if (authenticationModule == null)
  107. throw new ArgumentNullException ("authenticationModule");
  108. DoUnregister (authenticationModule.AuthenticationType, false);
  109. lock (modules)
  110. modules.Add (authenticationModule);
  111. }
  112. public static void Unregister (IAuthenticationModule authenticationModule)
  113. {
  114. if (authenticationModule == null)
  115. throw new ArgumentNullException ("authenticationModule");
  116. DoUnregister (authenticationModule.AuthenticationType, true);
  117. }
  118. public static void Unregister (string authenticationScheme)
  119. {
  120. if (authenticationScheme == null)
  121. throw new ArgumentNullException ("authenticationScheme");
  122. DoUnregister (authenticationScheme, true);
  123. }
  124. static void DoUnregister (string authenticationScheme, bool throwEx)
  125. {
  126. EnsureModules ();
  127. lock (modules) {
  128. IAuthenticationModule module = null;
  129. foreach (IAuthenticationModule mod in modules) {
  130. string modtype = mod.AuthenticationType;
  131. if (String.Compare (modtype, authenticationScheme, true) == 0) {
  132. module = mod;
  133. break;
  134. }
  135. }
  136. if (module == null) {
  137. if (throwEx)
  138. throw new InvalidOperationException ("Scheme not registered.");
  139. } else {
  140. modules.Remove (module);
  141. }
  142. }
  143. }
  144. }
  145. }