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.Collections.Specialized;
34 using System.Configuration;
35 using System.Net.Configuration;
39 public class AuthenticationManager {
40 static ArrayList modules;
41 static object locker = new object ();
43 private AuthenticationManager ()
47 static void EnsureModules ()
53 modules = new ArrayList ();
55 modules.Add (new NtlmClient ());
56 modules.Add (new DigestClient ());
57 modules.Add (new BasicClient ());
58 #elif CONFIGURATION_DEP
59 object cfg = ConfigurationManager.GetSection ("system.net/authenticationModules");
60 AuthenticationModulesSection s = cfg as AuthenticationModulesSection;
62 foreach (AuthenticationModuleElement element in s.AuthenticationModules) {
63 IAuthenticationModule module = null;
65 Type type = Type.GetType (element.Type, true);
66 module = (IAuthenticationModule) Activator.CreateInstance (type);
72 ConfigurationSettings.GetConfig ("system.net/authenticationModules");
77 static ICredentialPolicy credential_policy = null;
79 public static ICredentialPolicy CredentialPolicy
82 return(credential_policy);
85 credential_policy = value;
89 static Exception GetMustImplement ()
91 return new NotImplementedException ();
95 public static StringDictionary CustomTargetNameDictionary
98 throw GetMustImplement ();
102 public static IEnumerator RegisteredModules {
105 return modules.GetEnumerator ();
109 internal static void Clear ()
116 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
119 throw new ArgumentNullException ("request");
121 if (credentials == null)
122 throw new ArgumentNullException ("credentials");
124 if (challenge == null)
125 throw new ArgumentNullException ("challenge");
127 return DoAuthenticate (challenge, request, credentials);
130 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
134 foreach (IAuthenticationModule mod in modules) {
135 Authorization auth = mod.Authenticate (challenge, request, credentials);
147 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
150 throw new ArgumentNullException ("request");
152 if (credentials == null)
157 foreach (IAuthenticationModule mod in modules) {
158 Authorization auth = mod.PreAuthenticate (request, credentials);
170 public static void Register (IAuthenticationModule authenticationModule)
172 if (authenticationModule == null)
173 throw new ArgumentNullException ("authenticationModule");
175 DoUnregister (authenticationModule.AuthenticationType, false);
177 modules.Add (authenticationModule);
180 public static void Unregister (IAuthenticationModule authenticationModule)
182 if (authenticationModule == null)
183 throw new ArgumentNullException ("authenticationModule");
185 DoUnregister (authenticationModule.AuthenticationType, true);
188 public static void Unregister (string authenticationScheme)
190 if (authenticationScheme == null)
191 throw new ArgumentNullException ("authenticationScheme");
193 DoUnregister (authenticationScheme, true);
196 static void DoUnregister (string authenticationScheme, bool throwEx)
200 IAuthenticationModule module = null;
201 foreach (IAuthenticationModule mod in modules) {
202 string modtype = mod.AuthenticationType;
203 if (String.Compare (modtype, authenticationScheme, true) == 0) {
209 if (module == null) {
211 throw new InvalidOperationException ("Scheme not registered.");
213 modules.Remove (module);