2 // Mono.Http.NtlmClient
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) 2003 Novell, Inc. (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 extern alias MonoSecurity;
36 using System.Collections;
38 using MonoSecurity::Mono.Security.Protocol.Ntlm;
50 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
52 HttpWebRequest request = webRequest as HttpWebRequest;
56 NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
60 string userName = cred.UserName;
61 string domain = cred.Domain;
62 string password = cred.Password;
63 if (userName == null || userName == "")
66 if (String.IsNullOrEmpty (domain)) {
67 int idx = userName.IndexOf ('\\');
69 idx = userName.IndexOf ('/');
72 domain = userName.Substring (0, idx);
73 userName = userName.Substring (idx + 1);
77 bool completed = false;
78 if (message == null) {
79 Type1Message type1 = new Type1Message ();
80 type1.Domain = domain;
81 type1.Host = ""; // MS does not send it
82 type1.Flags |= NtlmFlags.NegotiateNtlm2Key;
84 } else if (message.Type == 1) {
85 // Should I check the credentials?
86 if (challenge == null) {
91 Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
95 Type3Message type3 = new Type3Message (type2);
96 type3.Username = userName;
97 type3.Password = password;
98 type3.Domain = domain;
102 // Should I check the credentials?
103 // type must be 3 here
104 if (challenge == null || challenge == String.Empty) {
105 Type1Message type1 = new Type1Message ();
106 type1.Domain = domain;
107 type1.Host = ""; // MS does not send it
114 string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
115 return new Authorization (token, completed);
119 class NtlmClient : IAuthenticationModule
121 static Hashtable cache;
125 cache = new Hashtable ();
128 public NtlmClient () {}
130 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
132 if (credentials == null || challenge == null)
135 string header = challenge.Trim ();
136 int idx = header.ToLower ().IndexOf ("ntlm");
140 idx = header.IndexOfAny (new char [] {' ', '\t'});
142 header = header.Substring (idx).Trim ();
147 HttpWebRequest request = webRequest as HttpWebRequest;
152 NtlmSession ds = (NtlmSession) cache [request];
154 ds = new NtlmSession ();
155 cache.Add (request, ds);
158 return ds.Authenticate (header, webRequest, credentials);
162 public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials)
167 public string AuthenticationType {
168 get { return "NTLM"; }
171 public bool CanPreAuthenticate {
172 get { return false; }