AuthenticationManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. private AuthenticationManager ()
  38. {
  39. }
  40. static void EnsureModules ()
  41. {
  42. if (modules != null)
  43. return;
  44. lock (typeof (AuthenticationManager)) {
  45. if (modules != null)
  46. return;
  47. modules = new ArrayList ();
  48. ConfigurationSettings.GetConfig ("system.net/authenticationModules");
  49. }
  50. }
  51. public static IEnumerator RegisteredModules {
  52. get {
  53. EnsureModules ();
  54. return modules.GetEnumerator ();
  55. }
  56. }
  57. internal static void Clear ()
  58. {
  59. EnsureModules ();
  60. lock (modules)
  61. modules.Clear ();
  62. }
  63. public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
  64. {
  65. if (request == null)
  66. throw new ArgumentNullException ("request");
  67. if (credentials == null)
  68. throw new ArgumentNullException ("credentials");
  69. if (challenge == null)
  70. throw new ArgumentNullException ("challenge");
  71. return DoAuthenticate (challenge, request, credentials);
  72. }
  73. static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
  74. {
  75. EnsureModules ();
  76. lock (modules) {
  77. foreach (IAuthenticationModule mod in modules) {
  78. Authorization auth = mod.Authenticate (challenge, request, credentials);
  79. if (auth == null)
  80. continue;
  81. auth.Module = mod;
  82. return auth;
  83. }
  84. }
  85. return null;
  86. }
  87. public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
  88. {
  89. if (request == null)
  90. throw new ArgumentNullException ("request");
  91. if (credentials == null)
  92. return null;
  93. EnsureModules ();
  94. lock (modules) {
  95. foreach (IAuthenticationModule mod in modules) {
  96. Authorization auth = mod.PreAuthenticate (request, credentials);
  97. if (auth == null)
  98. continue;
  99. auth.Module = mod;
  100. return auth;
  101. }
  102. }
  103. return null;
  104. }
  105. public static void Register (IAuthenticationModule authenticationModule)
  106. {
  107. if (authenticationModule == null)
  108. throw new ArgumentNullException ("authenticationModule");
  109. DoUnregister (authenticationModule.AuthenticationType, false);
  110. lock (modules)
  111. modules.Add (authenticationModule);
  112. }
  113. public static void Unregister (IAuthenticationModule authenticationModule)
  114. {
  115. if (authenticationModule == null)
  116. throw new ArgumentNullException ("authenticationModule");
  117. DoUnregister (authenticationModule.AuthenticationType, true);
  118. }
  119. public static void Unregister (string authenticationScheme)
  120. {
  121. if (authenticationScheme == null)
  122. throw new ArgumentNullException ("authenticationScheme");
  123. DoUnregister (authenticationScheme, true);
  124. }
  125. static void DoUnregister (string authenticationScheme, bool throwEx)
  126. {
  127. EnsureModules ();
  128. lock (modules) {
  129. IAuthenticationModule module = null;
  130. foreach (IAuthenticationModule mod in modules) {
  131. string modtype = mod.AuthenticationType;
  132. if (String.Compare (modtype, authenticationScheme, true) == 0) {
  133. module = mod;
  134. break;
  135. }
  136. }
  137. if (module == null) {
  138. if (throwEx)
  139. throw new InvalidOperationException ("Scheme not registered.");
  140. } else {
  141. modules.Remove (module);
  142. }
  143. }
  144. }
  145. }
  146. }