AuthenticationManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using System.Collections;
  11. using System.Configuration;
  12. namespace System.Net
  13. {
  14. public class AuthenticationManager
  15. {
  16. static ArrayList modules;
  17. private AuthenticationManager ()
  18. {
  19. }
  20. static void EnsureModules ()
  21. {
  22. if (modules != null)
  23. return;
  24. lock (typeof (AuthenticationManager)) {
  25. if (modules != null)
  26. return;
  27. modules = new ArrayList ();
  28. ConfigurationSettings.GetConfig ("system.net/authenticationModules");
  29. }
  30. }
  31. public static IEnumerator RegisteredModules {
  32. get {
  33. EnsureModules ();
  34. return modules.GetEnumerator ();
  35. }
  36. }
  37. internal static void Clear ()
  38. {
  39. EnsureModules ();
  40. lock (modules)
  41. modules.Clear ();
  42. }
  43. public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
  44. {
  45. if (request == null)
  46. throw new ArgumentNullException ("request");
  47. if (credentials == null)
  48. throw new ArgumentNullException ("credentials");
  49. if (challenge == null)
  50. throw new ArgumentNullException ("challenge");
  51. return DoAuthenticate (challenge, request, credentials);
  52. }
  53. static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
  54. {
  55. EnsureModules ();
  56. lock (modules) {
  57. foreach (IAuthenticationModule mod in modules) {
  58. Authorization auth = mod.Authenticate (challenge, request, credentials);
  59. if (auth == null)
  60. continue;
  61. auth.Module = mod;
  62. return auth;
  63. }
  64. }
  65. return null;
  66. }
  67. [MonoTODO]
  68. public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
  69. {
  70. if (request == null)
  71. throw new ArgumentNullException ("request");
  72. if (credentials == null)
  73. return null;
  74. return null;
  75. }
  76. public static void Register (IAuthenticationModule authenticationModule)
  77. {
  78. if (authenticationModule == null)
  79. throw new ArgumentNullException ("authenticationModule");
  80. DoUnregister (authenticationModule.AuthenticationType, false);
  81. lock (modules)
  82. modules.Add (authenticationModule);
  83. }
  84. public static void Unregister (IAuthenticationModule authenticationModule)
  85. {
  86. if (authenticationModule == null)
  87. throw new ArgumentNullException ("authenticationModule");
  88. DoUnregister (authenticationModule.AuthenticationType, true);
  89. }
  90. public static void Unregister (string authenticationScheme)
  91. {
  92. if (authenticationScheme == null)
  93. throw new ArgumentNullException ("authenticationScheme");
  94. DoUnregister (authenticationScheme, true);
  95. }
  96. static void DoUnregister (string authenticationScheme, bool throwEx)
  97. {
  98. EnsureModules ();
  99. lock (modules) {
  100. IAuthenticationModule module = null;
  101. foreach (IAuthenticationModule mod in modules) {
  102. string modtype = mod.AuthenticationType;
  103. if (String.Compare (modtype, authenticationScheme, true) == 0) {
  104. module = mod;
  105. break;
  106. }
  107. }
  108. if (module == null) {
  109. if (throwEx)
  110. throw new InvalidOperationException ("Scheme not registered.");
  111. } else {
  112. modules.Remove (module);
  113. }
  114. }
  115. }
  116. }
  117. }