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.
34 using Mono.Security.Protocol.Ntlm;
36 extern alias MonoSecurity;
37 using MonoSecurity::Mono.Security.Protocol.Ntlm;
41 using System.Collections;
54 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
56 HttpWebRequest request = webRequest as HttpWebRequest;
60 NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
64 string userName = cred.UserName;
65 string domain = cred.Domain;
66 string password = cred.Password;
67 if (userName == null || userName == "")
70 if (String.IsNullOrEmpty (domain)) {
71 int idx = userName.IndexOf ('\\');
73 idx = userName.IndexOf ('/');
76 domain = userName.Substring (0, idx);
77 userName = userName.Substring (idx + 1);
81 bool completed = false;
82 if (message == null) {
83 Type1Message type1 = new Type1Message ();
84 type1.Domain = domain;
85 type1.Host = ""; // MS does not send it
86 type1.Flags |= NtlmFlags.NegotiateNtlm2Key;
88 } else if (message.Type == 1) {
89 // Should I check the credentials?
90 if (challenge == null) {
95 Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
99 Type3Message type3 = new Type3Message (type2);
100 type3.Username = userName;
101 type3.Password = password;
102 type3.Domain = domain;
106 // Should I check the credentials?
107 // type must be 3 here
108 if (challenge == null || challenge == String.Empty) {
109 Type1Message type1 = new Type1Message ();
110 type1.Domain = domain;
111 type1.Host = ""; // MS does not send it
118 string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
119 return new Authorization (token, completed);
123 class NtlmClient : IAuthenticationModule
125 static Hashtable cache;
129 cache = new Hashtable ();
132 public NtlmClient () {}
134 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
136 if (credentials == null || challenge == null)
139 string header = challenge.Trim ();
140 int idx = header.ToLower ().IndexOf ("ntlm");
144 idx = header.IndexOfAny (new char [] {' ', '\t'});
146 header = header.Substring (idx).Trim ();
151 HttpWebRequest request = webRequest as HttpWebRequest;
156 NtlmSession ds = (NtlmSession) cache [request];
158 ds = new NtlmSession ();
159 cache.Add (request, ds);
162 return ds.Authenticate (header, webRequest, credentials);
166 public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials)
171 public string AuthenticationType {
172 get { return "NTLM"; }
175 public bool CanPreAuthenticate {
176 get { return false; }