2 // System.Net.AuthenticationManager.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Configuration;
37 public class AuthenticationManager
39 static ArrayList modules;
41 private AuthenticationManager ()
45 static void EnsureModules ()
50 lock (typeof (AuthenticationManager)) {
54 modules = new ArrayList ();
55 ConfigurationSettings.GetConfig ("system.net/authenticationModules");
59 public static IEnumerator RegisteredModules {
62 return modules.GetEnumerator ();
66 internal static void Clear ()
73 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
76 throw new ArgumentNullException ("request");
78 if (credentials == null)
79 throw new ArgumentNullException ("credentials");
81 if (challenge == null)
82 throw new ArgumentNullException ("challenge");
84 return DoAuthenticate (challenge, request, credentials);
87 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
91 foreach (IAuthenticationModule mod in modules) {
92 Authorization auth = mod.Authenticate (challenge, request, credentials);
104 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
107 throw new ArgumentNullException ("request");
109 if (credentials == null)
114 foreach (IAuthenticationModule mod in modules) {
115 Authorization auth = mod.PreAuthenticate (request, credentials);
127 public static void Register (IAuthenticationModule authenticationModule)
129 if (authenticationModule == null)
130 throw new ArgumentNullException ("authenticationModule");
132 DoUnregister (authenticationModule.AuthenticationType, false);
134 modules.Add (authenticationModule);
137 public static void Unregister (IAuthenticationModule authenticationModule)
139 if (authenticationModule == null)
140 throw new ArgumentNullException ("authenticationModule");
142 DoUnregister (authenticationModule.AuthenticationType, true);
145 public static void Unregister (string authenticationScheme)
147 if (authenticationScheme == null)
148 throw new ArgumentNullException ("authenticationScheme");
150 DoUnregister (authenticationScheme, true);
153 static void DoUnregister (string authenticationScheme, bool throwEx)
157 IAuthenticationModule module = null;
158 foreach (IAuthenticationModule mod in modules) {
159 string modtype = mod.AuthenticationType;
160 if (String.Compare (modtype, authenticationScheme, true) == 0) {
166 if (module == null) {
168 throw new InvalidOperationException ("Scheme not registered.");
170 modules.Remove (module);