We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Net
- {
- using System.Text;
- using System.Globalization;
- using System.Security.Permissions;
-
- internal class BasicClient : IAuthenticationModule
- {
-
- internal const string AuthType = "Basic";
- static internal string Signature = AuthType.ToLower(CultureInfo.InvariantCulture);
- static internal int SignatureSize = Signature.Length;
-
- public Authorization Authenticate(string challenge, WebRequest webRequest, ICredentials credentials)
- {
- GlobalLog.Print("BasicClient::Authenticate(): " + challenge);
-
- GlobalLog.Assert(credentials != null, "BasicClient::Authenticate()|credentials == null");
- if (credentials == null) {
- return null;
- }
-
- HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
-
- GlobalLog.Assert(httpWebRequest != null, "BasicClient::Authenticate()|httpWebRequest == null");
- if (httpWebRequest == null || httpWebRequest.ChallengedUri == null) {
-
-
-
-
-
- return null;
- }
-
- int index = AuthenticationManager.FindSubstringNotInQuotes(challenge, Signature);
- if (index < 0) {
- return null;
- }
-
- return Lookup(httpWebRequest, credentials);
- }
-
- public bool CanPreAuthenticate {
- get { return true; }
- }
-
- public Authorization PreAuthenticate(WebRequest webRequest, ICredentials credentials)
- {
- GlobalLog.Print("BasicClient::PreAuthenticate()");
-
- GlobalLog.Assert(credentials != null, "BasicClient::Authenticate()|credentials == null");
- if (credentials == null) {
- return null;
- }
-
- HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
-
- GlobalLog.Assert(httpWebRequest != null, "BasicClient::Authenticate()|httpWebRequest == null");
- if (httpWebRequest == null) {
- return null;
- }
-
- return Lookup(httpWebRequest, credentials);
- }
-
- public string AuthenticationType {
- get { return AuthType; }
- }
-
-
- private Authorization Lookup(HttpWebRequest httpWebRequest, ICredentials credentials)
- {
- GlobalLog.Print("BasicClient::Lookup(): ChallengedUri:" + httpWebRequest.ChallengedUri.ToString());
-
- NetworkCredential NC = credentials.GetCredential(httpWebRequest.ChallengedUri, Signature);
- GlobalLog.Print("BasicClient::Lookup() GetCredential() returns:" + ValidationHelper.ToString(NC));
-
- if (NC == null) {
- return null;
- }
-
- ICredentialPolicy policy = AuthenticationManager.CredentialPolicy;
- if (policy != null && !policy.ShouldSendCredential(httpWebRequest.ChallengedUri, httpWebRequest, NC, this))
- return null;
-
-
- string username = NC.InternalGetUserName();
- string domain = NC.InternalGetDomain();
-
- if (ValidationHelper.IsBlankString(username)) {
- return null;
- }
-
- string rawString = ((!ValidationHelper.IsBlankString(domain)) ? (domain + "\\") : "") + username + ":" + NC.InternalGetPassword();
-
-
-
-
- byte[] bytes = EncodingRightGetBytes(rawString);
- string responseHeader = BasicClient.AuthType + " " + Convert.ToBase64String(bytes);
-
- return new Authorization(responseHeader, true);
- }
-
- static internal byte[] EncodingRightGetBytes(string rawString)
- {
- GlobalLog.Enter("BasicClient::EncodingRightGetBytes", "[" + rawString.Length.ToString() + ":" + rawString + "]");
-
-
-
-
-
- GlobalLog.Print("BasicClient::EncodingRightGetBytes(): Default Encoding is:" + Encoding.Default.EncodingName);
-
- byte[] bytes = Encoding.Default.GetBytes(rawString);
- string rawCopy = Encoding.Default.GetString(bytes);
- bool canMapToCurrentCodePage = string.Compare(rawString, rawCopy, StringComparison.Ordinal) == 0;
-
- GlobalLog.Print("BasicClient::EncodingRightGetBytes(): canMapToCurrentCodePage:" + canMapToCurrentCodePage.ToString());
-
-
- if (!canMapToCurrentCodePage) {
-
- GlobalLog.LeaveException("BasicClient::EncodingRightGetBytes", ExceptionHelper.MethodNotSupportedException);
- throw ExceptionHelper.MethodNotSupportedException;
-
-
- }
-
- GlobalLog.Dump(bytes);
- GlobalLog.Leave("BasicClient::EncodingRightGetBytes", bytes.Length.ToString());
-
- return bytes;
- }
-
- }
-
-
- }