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 using System.Collections;
35 using Mono.Security.Protocol.Ntlm;
47 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
49 HttpWebRequest request = webRequest as HttpWebRequest;
53 NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
57 string userName = cred.UserName;
58 string domain = cred.Domain;
59 string password = cred.Password;
60 if (userName == null || userName == "")
62 domain = domain != null && domain.Length > 0 ? domain : request.Headers ["Host"];
64 bool completed = false;
65 if (message == null) {
66 Type1Message type1 = new Type1Message ();
67 type1.Domain = domain;
69 } else if (message.Type == 1) {
70 // Should I check the credentials?
71 if (challenge == null) {
76 Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
80 Type3Message type3 = new Type3Message ();
81 type3.Domain = domain;
82 type3.Username = userName;
83 type3.Challenge = type2.Nonce;
84 type3.Password = password;
88 // Should I check the credentials?
89 // type must be 3 here
90 if (challenge == null || challenge == String.Empty) {
91 Type1Message type1 = new Type1Message ();
92 type1.Domain = domain;
99 string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
100 return new Authorization (token, completed);
104 class NtlmClient : IAuthenticationModule
106 static Hashtable cache;
110 cache = new Hashtable ();
113 public NtlmClient () {}
115 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
117 if (credentials == null || challenge == null)
120 string header = challenge.Trim ();
121 int idx = header.ToLower ().IndexOf ("ntlm");
125 idx = header.IndexOfAny (new char [] {' ', '\t'});
127 header = header.Substring (idx).Trim ();
132 HttpWebRequest request = webRequest as HttpWebRequest;
137 NtlmSession ds = (NtlmSession) cache [request.RequestUri];
139 ds = new NtlmSession ();
140 cache.Add (request.RequestUri, ds);
143 return ds.Authenticate (header, webRequest, credentials);
147 public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials)
152 public string AuthenticationType {
153 get { return "NTLM"; }
156 public bool CanPreAuthenticate {
157 get { return false; }