The Labs \ Source Viewer \ SSCLI \ System.Net \ BasicClient

  1. //------------------------------------------------------------------------------
  2. // <copyright file="_BasicClient.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net
  16. {
  17.     using System.Text;
  18.     using System.Globalization;
  19.     using System.Security.Permissions;
  20.    
  21.     internal class BasicClient : IAuthenticationModule
  22.     {
  23.        
  24.         internal const string AuthType = "Basic";
  25.         static internal string Signature = AuthType.ToLower(CultureInfo.InvariantCulture);
  26.         static internal int SignatureSize = Signature.Length;
  27.        
  28.         public Authorization Authenticate(string challenge, WebRequest webRequest, ICredentials credentials)
  29.         {
  30.             GlobalLog.Print("BasicClient::Authenticate(): " + challenge);
  31.            
  32.             GlobalLog.Assert(credentials != null, "BasicClient::Authenticate()|credentials == null");
  33.             if (credentials == null) {
  34.                 return null;
  35.             }
  36.            
  37.             HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
  38.            
  39.             GlobalLog.Assert(httpWebRequest != null, "BasicClient::Authenticate()|httpWebRequest == null");
  40.             if (httpWebRequest == null || httpWebRequest.ChallengedUri == null) {
  41.                 //
  42.                 // there has been no challenge:
  43.                 // 1) the request never went on the wire
  44.                 // 2) somebody other than us is calling into AuthenticationManager
  45.                 //
  46.                 return null;
  47.             }
  48.            
  49.             int index = AuthenticationManager.FindSubstringNotInQuotes(challenge, Signature);
  50.             if (index < 0) {
  51.                 return null;
  52.             }
  53.            
  54.             return Lookup(httpWebRequest, credentials);
  55.         }
  56.        
  57.         public bool CanPreAuthenticate {
  58.             get { return true; }
  59.         }
  60.        
  61.         public Authorization PreAuthenticate(WebRequest webRequest, ICredentials credentials)
  62.         {
  63.             GlobalLog.Print("BasicClient::PreAuthenticate()");
  64.            
  65.             GlobalLog.Assert(credentials != null, "BasicClient::Authenticate()|credentials == null");
  66.             if (credentials == null) {
  67.                 return null;
  68.             }
  69.            
  70.             HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
  71.            
  72.             GlobalLog.Assert(httpWebRequest != null, "BasicClient::Authenticate()|httpWebRequest == null");
  73.             if (httpWebRequest == null) {
  74.                 return null;
  75.             }
  76.            
  77.             return Lookup(httpWebRequest, credentials);
  78.         }
  79.        
  80.         public string AuthenticationType {
  81.             get { return AuthType; }
  82.         }
  83.        
  84.        
  85.         private Authorization Lookup(HttpWebRequest httpWebRequest, ICredentials credentials)
  86.         {
  87.             GlobalLog.Print("BasicClient::Lookup(): ChallengedUri:" + httpWebRequest.ChallengedUri.ToString());
  88.            
  89.             NetworkCredential NC = credentials.GetCredential(httpWebRequest.ChallengedUri, Signature);
  90.             GlobalLog.Print("BasicClient::Lookup() GetCredential() returns:" + ValidationHelper.ToString(NC));
  91.            
  92.             if (NC == null) {
  93.                 return null;
  94.             }
  95.            
  96.             ICredentialPolicy policy = AuthenticationManager.CredentialPolicy;
  97.             if (policy != null && !policy.ShouldSendCredential(httpWebRequest.ChallengedUri, httpWebRequest, NC, this))
  98.                 return null;
  99.            
  100.            
  101.             string username = NC.InternalGetUserName();
  102.             string domain = NC.InternalGetDomain();
  103.            
  104.             if (ValidationHelper.IsBlankString(username)) {
  105.                 return null;
  106.             }
  107.            
  108.             string rawString = ((!ValidationHelper.IsBlankString(domain)) ? (domain + "\\") : "") + username + ":" + NC.InternalGetPassword();
  109.            
  110.             // The response is an "Authorization:" header where the value is
  111.             // the text "Basic" followed by BASE64 encoded (as defined by RFC1341) value
  112.            
  113.             byte[] bytes = EncodingRightGetBytes(rawString);
  114.             string responseHeader = BasicClient.AuthType + " " + Convert.ToBase64String(bytes);
  115.            
  116.             return new Authorization(responseHeader, true);
  117.         }
  118.        
  119.         static internal byte[] EncodingRightGetBytes(string rawString)
  120.         {
  121.             GlobalLog.Enter("BasicClient::EncodingRightGetBytes", "[" + rawString.Length.ToString() + ":" + rawString + "]");
  122.             //
  123.             // in order to know if there will not be any '?' translations (which means
  124.             // we should use the Default Encoding) we need to attempt encoding and then decoding.
  125.             //
  126.             //
  127.             GlobalLog.Print("BasicClient::EncodingRightGetBytes(): Default Encoding is:" + Encoding.Default.EncodingName);
  128.            
  129.             byte[] bytes = Encoding.Default.GetBytes(rawString);
  130.             string rawCopy = Encoding.Default.GetString(bytes);
  131.             bool canMapToCurrentCodePage = string.Compare(rawString, rawCopy, StringComparison.Ordinal) == 0;
  132.            
  133.             GlobalLog.Print("BasicClient::EncodingRightGetBytes(): canMapToCurrentCodePage:" + canMapToCurrentCodePage.ToString());
  134.            
  135.             //
  136.             if (!canMapToCurrentCodePage) {
  137.                 //
  138.                 GlobalLog.LeaveException("BasicClient::EncodingRightGetBytes", ExceptionHelper.MethodNotSupportedException);
  139.                 throw ExceptionHelper.MethodNotSupportedException;
  140.                 /*
  141.                 GlobalLog.Print("BasicClient::EncodingRightGetBytes(): using:" + Encoding.UTF8.EncodingName);
  142.                 bytes = Encoding.UTF8.GetBytes(rawString);
  143.                 string blob = "=?utf-8?B?" + Convert.ToBase64String(bytes) + "?=";
  144.                 bytes = Encoding.ASCII.GetBytes(blob);
  145.                 */               
  146.             }
  147.            
  148.             GlobalLog.Dump(bytes);
  149.             GlobalLog.Leave("BasicClient::EncodingRightGetBytes", bytes.Length.ToString());
  150.            
  151.             return bytes;
  152.         }
  153.        
  154.     }
  155.     // class BasicClient
  156.    
  157. }
  158. // namespace System.Net

Developer Fusion