Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Net / System / Net / _BasicClient.cs / 1305376 / _BasicClient.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Net {
using System.Text;
using System.Globalization;
using System.Security.Permissions;
internal class BasicClient : IAuthenticationModule {
internal const string AuthType = "Basic";
internal static string Signature = AuthType.ToLower(CultureInfo.InvariantCulture);
internal static 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 !FEATURE_PAL
if (credentials==null || credentials is SystemNetworkCredential) {
#else
if (credentials==null) {
#endif // !FEATURE_PAL
return null;
}
HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
GlobalLog.Assert(httpWebRequest != null, "BasicClient::Authenticate()|httpWebRequest == null");
if (httpWebRequest==null || httpWebRequest.ChallengedUri==null) {
//
// there has been no challenge:
// 1) the request never went on the wire
// 2) somebody other than us is calling into AuthenticationManager
//
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 !FEATURE_PAL
if (credentials==null || credentials is SystemNetworkCredential) {
#else
if (credentials==null) {
#endif // !FEATURE_PAL
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();
// The response is an "Authorization:" header where the value is
// the text "Basic" followed by BASE64 encoded (as defined by RFC1341) value
byte[] bytes = EncodingRightGetBytes(rawString);
string responseHeader = BasicClient.AuthType + " " + Convert.ToBase64String(bytes);
return new Authorization(responseHeader, true);
}
internal static byte[] EncodingRightGetBytes(string rawString) {
GlobalLog.Enter("BasicClient::EncodingRightGetBytes", "[" + rawString.Length.ToString() + ":" + rawString + "]");
//
// in order to know if there will not be any '?' translations (which means
// we should use the Default Encoding) we need to attempt encoding and then decoding.
// this is a limitation only on win9x, if we ever drop support for this platform there might be
// a more efficient way of doing this.
//
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 mapping to the current code page leaves characters out of the
// [0x00, 0xFF] range, then we need to use the new encoding that IIS6.0
// will support. do it when they decide it's good enough.
//
if (!canMapToCurrentCodePage) {
//
// for now throw. when IIS 6.0 adds support test it.
//
GlobalLog.LeaveException("BasicClient::EncodingRightGetBytes", ExceptionHelper.MethodNotSupportedException);
throw ExceptionHelper.MethodNotSupportedException;
/*
GlobalLog.Print("BasicClient::EncodingRightGetBytes(): using:" + Encoding.UTF8.EncodingName);
bytes = Encoding.UTF8.GetBytes(rawString);
string blob = "=?utf-8?B?" + Convert.ToBase64String(bytes) + "?=";
bytes = Encoding.ASCII.GetBytes(blob);
*/
}
GlobalLog.Dump(bytes);
GlobalLog.Leave("BasicClient::EncodingRightGetBytes", bytes.Length.ToString());
return bytes;
}
}; // class BasicClient
} // namespace System.Net
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Net {
using System.Text;
using System.Globalization;
using System.Security.Permissions;
internal class BasicClient : IAuthenticationModule {
internal const string AuthType = "Basic";
internal static string Signature = AuthType.ToLower(CultureInfo.InvariantCulture);
internal static 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 !FEATURE_PAL
if (credentials==null || credentials is SystemNetworkCredential) {
#else
if (credentials==null) {
#endif // !FEATURE_PAL
return null;
}
HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
GlobalLog.Assert(httpWebRequest != null, "BasicClient::Authenticate()|httpWebRequest == null");
if (httpWebRequest==null || httpWebRequest.ChallengedUri==null) {
//
// there has been no challenge:
// 1) the request never went on the wire
// 2) somebody other than us is calling into AuthenticationManager
//
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 !FEATURE_PAL
if (credentials==null || credentials is SystemNetworkCredential) {
#else
if (credentials==null) {
#endif // !FEATURE_PAL
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();
// The response is an "Authorization:" header where the value is
// the text "Basic" followed by BASE64 encoded (as defined by RFC1341) value
byte[] bytes = EncodingRightGetBytes(rawString);
string responseHeader = BasicClient.AuthType + " " + Convert.ToBase64String(bytes);
return new Authorization(responseHeader, true);
}
internal static byte[] EncodingRightGetBytes(string rawString) {
GlobalLog.Enter("BasicClient::EncodingRightGetBytes", "[" + rawString.Length.ToString() + ":" + rawString + "]");
//
// in order to know if there will not be any '?' translations (which means
// we should use the Default Encoding) we need to attempt encoding and then decoding.
// this is a limitation only on win9x, if we ever drop support for this platform there might be
// a more efficient way of doing this.
//
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 mapping to the current code page leaves characters out of the
// [0x00, 0xFF] range, then we need to use the new encoding that IIS6.0
// will support. do it when they decide it's good enough.
//
if (!canMapToCurrentCodePage) {
//
// for now throw. when IIS 6.0 adds support test it.
//
GlobalLog.LeaveException("BasicClient::EncodingRightGetBytes", ExceptionHelper.MethodNotSupportedException);
throw ExceptionHelper.MethodNotSupportedException;
/*
GlobalLog.Print("BasicClient::EncodingRightGetBytes(): using:" + Encoding.UTF8.EncodingName);
bytes = Encoding.UTF8.GetBytes(rawString);
string blob = "=?utf-8?B?" + Convert.ToBase64String(bytes) + "?=";
bytes = Encoding.ASCII.GetBytes(blob);
*/
}
GlobalLog.Dump(bytes);
GlobalLog.Leave("BasicClient::EncodingRightGetBytes", bytes.Length.ToString());
return bytes;
}
}; // class BasicClient
} // namespace System.Net
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Form.cs
- COAUTHIDENTITY.cs
- ViewValidator.cs
- TextEditorCopyPaste.cs
- DataObjectAttribute.cs
- MatrixTransform3D.cs
- InvalidCommandTreeException.cs
- PnrpPermission.cs
- ControlPersister.cs
- QilCloneVisitor.cs
- StringFormat.cs
- QuaternionRotation3D.cs
- PageThemeCodeDomTreeGenerator.cs
- CalendarAutomationPeer.cs
- EntityDataSourceWrapper.cs
- FileClassifier.cs
- DataGridCell.cs
- DataGridViewSortCompareEventArgs.cs
- IISMapPath.cs
- BufferAllocator.cs
- XamlFigureLengthSerializer.cs
- DataGridViewComponentPropertyGridSite.cs
- ChannelParameterCollection.cs
- FormViewCommandEventArgs.cs
- ProxyElement.cs
- TableCell.cs
- BitmapCacheBrush.cs
- DataServiceQuery.cs
- RoleGroupCollection.cs
- IdnElement.cs
- ObjectHandle.cs
- ParameterCollection.cs
- OleServicesContext.cs
- TextDpi.cs
- ToolboxService.cs
- ResourceReader.cs
- DataSourceView.cs
- IsolatedStorageFileStream.cs
- GlyphInfoList.cs
- XmlSchemaIdentityConstraint.cs
- ThrowHelper.cs
- DesigntimeLicenseContextSerializer.cs
- ParsedAttributeCollection.cs
- unsafenativemethodstextservices.cs
- ServiceDeploymentInfo.cs
- OLEDB_Enum.cs
- ScriptManagerProxy.cs
- CardSpaceSelector.cs
- RadioButtonList.cs
- InvokeProviderWrapper.cs
- GeneralTransform2DTo3DTo2D.cs
- SimpleType.cs
- _HeaderInfoTable.cs
- StrongNameIdentityPermission.cs
- safex509handles.cs
- HttpsTransportBindingElement.cs
- SoapIgnoreAttribute.cs
- InternalConfigSettingsFactory.cs
- CompilerInfo.cs
- UIElementAutomationPeer.cs
- GlobalAllocSafeHandle.cs
- TypeElement.cs
- TextElement.cs
- TextParagraphCache.cs
- EventLogEntryCollection.cs
- ActivityXRefConverter.cs
- ThreadPoolTaskScheduler.cs
- DefaultShape.cs
- OracleNumber.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- ImageDrawing.cs
- TwoPhaseCommitProxy.cs
- CodeConditionStatement.cs
- WindowsListViewGroupSubsetLink.cs
- WindowsGraphicsWrapper.cs
- SpotLight.cs
- ToolStripItemEventArgs.cs
- NonParentingControl.cs
- XsltCompileContext.cs
- HtmlInputControl.cs
- ObjectAnimationBase.cs
- HwndMouseInputProvider.cs
- BitmapEffectGroup.cs
- HtmlTable.cs
- DataRowCollection.cs
- TimelineGroup.cs
- UTF32Encoding.cs
- DocumentSchemaValidator.cs
- EnvelopedPkcs7.cs
- MarginsConverter.cs
- BitmapData.cs
- SafeMILHandleMemoryPressure.cs
- AccessViolationException.cs
- CfgSemanticTag.cs
- SaveFileDialog.cs
- PathSegment.cs
- PackageProperties.cs
- ScrollPattern.cs
- RemotingConfiguration.cs
- HostedTcpTransportManager.cs