Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / Net / System / Net / _ServiceNameStore.cs / 2 / _ServiceNameStore.cs
using System; using System.Collections.Generic; using System.Security.Authentication.ExtendedProtection; using System.Diagnostics; namespace System.Net { internal class ServiceNameStore { private ListserviceNames; private ServiceNameCollection serviceNameCollection; public ServiceNameCollection ServiceNames { get { if (serviceNameCollection == null) { serviceNameCollection = new ServiceNameCollection(serviceNames); } return serviceNameCollection; } } public ServiceNameStore() { serviceNames = new List (); serviceNameCollection = null; // set only when needed (due to expensive item-by-item copy) } private bool AddSingleServiceName(string spn) { if (Contains(spn)) { return false; } else { serviceNames.Add(spn); return true; } } public bool Add(string uriPrefix) { Debug.Assert(!String.IsNullOrEmpty(uriPrefix)); string[] newServiceNames = BuildServiceNames(uriPrefix); bool addedAny = false; foreach (string spn in newServiceNames) { if (AddSingleServiceName(spn)) { addedAny = true; if (Logging.On) { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Add() adding default SPNs '" + spn + "' from prefix '" + uriPrefix + "'"); } } } if (addedAny) { serviceNameCollection = null; } else if (Logging.On) { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Add() no default SPN added for prefix '" + uriPrefix + "'"); } return addedAny; } public bool Remove(string uriPrefix) { Debug.Assert(!String.IsNullOrEmpty(uriPrefix)); string newServiceName = BuildSimpleServiceName(uriPrefix); bool needToRemove = Contains(newServiceName); if (needToRemove) { serviceNames.Remove(newServiceName); serviceNameCollection = null; //invalidate (readonly) ServiceNameCollection } if (Logging.On) { if (needToRemove) { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Remove() removing default SPN '" + newServiceName + "' from prefix '" + uriPrefix + "'"); } else { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Remove() no default SPN removed for prefix '" + uriPrefix + "'"); } } return needToRemove; } private bool Contains(string newServiceName) { if (newServiceName == null) { return false; } bool found = false; foreach (string serviceName in serviceNames) { if (String.Compare(serviceName, newServiceName, StringComparison.InvariantCultureIgnoreCase) == 0) { found = true; break; } } return found; } public void Clear() { serviceNames.Clear(); serviceNameCollection = null; //invalidate (readonly) ServiceNameCollection } private string ExtractHostname(string uriPrefix, bool allowInvalidUriStrings) { if (Uri.IsWellFormedUriString(uriPrefix, UriKind.Absolute)) { Uri hostUri = new Uri(uriPrefix); return hostUri.Host; } else if (allowInvalidUriStrings) { int i = uriPrefix.IndexOf("://") + 3; int j = i; bool inSquareBrackets = false; while(j < uriPrefix.Length && uriPrefix[j] != '/' && (uriPrefix[j] != ':' || inSquareBrackets)) { if (uriPrefix[j] == '[') { if (inSquareBrackets) { j = i; break; } inSquareBrackets = true; } if (inSquareBrackets && uriPrefix[j] == ']') { inSquareBrackets = false; } j++; } return uriPrefix.Substring(i, j - i); } return null; } public string BuildSimpleServiceName(string uriPrefix) { string hostname = ExtractHostname(uriPrefix, false); if (hostname != null) { return "HTTP/" + hostname; } else { return null; } } public string[] BuildServiceNames(string uriPrefix) { string hostname = ExtractHostname(uriPrefix, true); IPAddress ipAddress = null; if (String.Compare(hostname, "*", StringComparison.InvariantCultureIgnoreCase) == 0 || String.Compare(hostname, "+", StringComparison.InvariantCultureIgnoreCase) == 0 || IPAddress.TryParse(hostname, out ipAddress)) { // for a wildcard, register the machine name. If the caller doesn't have DNS permission // or the query fails for some reason, don't add an SPN. try { string machineName = Dns.GetHostEntry(String.Empty).HostName; return new string[] { "HTTP/" + machineName }; } catch (System.Net.Sockets.SocketException) { return new string[0]; } catch (System.Security.SecurityException) { return new string[0]; } } else if (!hostname.Contains(".")) { // for a dotless name, try to resolve the FQDN. If the caller doesn't have DNS permission // or the query fails for some reason, add only the dotless name. try { string fqdn = Dns.GetHostEntry(hostname).HostName; return new string[] { "HTTP/" + hostname, "HTTP/" + fqdn }; } catch (System.Net.Sockets.SocketException) { return new string[] { "HTTP/" + hostname }; } catch (System.Security.SecurityException) { return new string[] { "HTTP/" + hostname }; } } else { return new string[] { "HTTP/" + hostname }; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Collections.Generic; using System.Security.Authentication.ExtendedProtection; using System.Diagnostics; namespace System.Net { internal class ServiceNameStore { private List serviceNames; private ServiceNameCollection serviceNameCollection; public ServiceNameCollection ServiceNames { get { if (serviceNameCollection == null) { serviceNameCollection = new ServiceNameCollection(serviceNames); } return serviceNameCollection; } } public ServiceNameStore() { serviceNames = new List (); serviceNameCollection = null; // set only when needed (due to expensive item-by-item copy) } private bool AddSingleServiceName(string spn) { if (Contains(spn)) { return false; } else { serviceNames.Add(spn); return true; } } public bool Add(string uriPrefix) { Debug.Assert(!String.IsNullOrEmpty(uriPrefix)); string[] newServiceNames = BuildServiceNames(uriPrefix); bool addedAny = false; foreach (string spn in newServiceNames) { if (AddSingleServiceName(spn)) { addedAny = true; if (Logging.On) { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Add() adding default SPNs '" + spn + "' from prefix '" + uriPrefix + "'"); } } } if (addedAny) { serviceNameCollection = null; } else if (Logging.On) { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Add() no default SPN added for prefix '" + uriPrefix + "'"); } return addedAny; } public bool Remove(string uriPrefix) { Debug.Assert(!String.IsNullOrEmpty(uriPrefix)); string newServiceName = BuildSimpleServiceName(uriPrefix); bool needToRemove = Contains(newServiceName); if (needToRemove) { serviceNames.Remove(newServiceName); serviceNameCollection = null; //invalidate (readonly) ServiceNameCollection } if (Logging.On) { if (needToRemove) { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Remove() removing default SPN '" + newServiceName + "' from prefix '" + uriPrefix + "'"); } else { Logging.PrintInfo(Logging.HttpListener, "ServiceNameStore#" + ValidationHelper.HashString(this) + "::Remove() no default SPN removed for prefix '" + uriPrefix + "'"); } } return needToRemove; } private bool Contains(string newServiceName) { if (newServiceName == null) { return false; } bool found = false; foreach (string serviceName in serviceNames) { if (String.Compare(serviceName, newServiceName, StringComparison.InvariantCultureIgnoreCase) == 0) { found = true; break; } } return found; } public void Clear() { serviceNames.Clear(); serviceNameCollection = null; //invalidate (readonly) ServiceNameCollection } private string ExtractHostname(string uriPrefix, bool allowInvalidUriStrings) { if (Uri.IsWellFormedUriString(uriPrefix, UriKind.Absolute)) { Uri hostUri = new Uri(uriPrefix); return hostUri.Host; } else if (allowInvalidUriStrings) { int i = uriPrefix.IndexOf("://") + 3; int j = i; bool inSquareBrackets = false; while(j < uriPrefix.Length && uriPrefix[j] != '/' && (uriPrefix[j] != ':' || inSquareBrackets)) { if (uriPrefix[j] == '[') { if (inSquareBrackets) { j = i; break; } inSquareBrackets = true; } if (inSquareBrackets && uriPrefix[j] == ']') { inSquareBrackets = false; } j++; } return uriPrefix.Substring(i, j - i); } return null; } public string BuildSimpleServiceName(string uriPrefix) { string hostname = ExtractHostname(uriPrefix, false); if (hostname != null) { return "HTTP/" + hostname; } else { return null; } } public string[] BuildServiceNames(string uriPrefix) { string hostname = ExtractHostname(uriPrefix, true); IPAddress ipAddress = null; if (String.Compare(hostname, "*", StringComparison.InvariantCultureIgnoreCase) == 0 || String.Compare(hostname, "+", StringComparison.InvariantCultureIgnoreCase) == 0 || IPAddress.TryParse(hostname, out ipAddress)) { // for a wildcard, register the machine name. If the caller doesn't have DNS permission // or the query fails for some reason, don't add an SPN. try { string machineName = Dns.GetHostEntry(String.Empty).HostName; return new string[] { "HTTP/" + machineName }; } catch (System.Net.Sockets.SocketException) { return new string[0]; } catch (System.Security.SecurityException) { return new string[0]; } } else if (!hostname.Contains(".")) { // for a dotless name, try to resolve the FQDN. If the caller doesn't have DNS permission // or the query fails for some reason, add only the dotless name. try { string fqdn = Dns.GetHostEntry(hostname).HostName; return new string[] { "HTTP/" + hostname, "HTTP/" + fqdn }; } catch (System.Net.Sockets.SocketException) { return new string[] { "HTTP/" + hostname }; } catch (System.Security.SecurityException) { return new string[] { "HTTP/" + hostname }; } } else { return new string[] { "HTTP/" + hostname }; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- LookupNode.cs
- ArrayConverter.cs
- SendingRequestEventArgs.cs
- UserPreferenceChangedEventArgs.cs
- ParentUndoUnit.cs
- EventRouteFactory.cs
- EntityCommandCompilationException.cs
- TogglePattern.cs
- DbParameterHelper.cs
- WebZone.cs
- FactoryMaker.cs
- CLSCompliantAttribute.cs
- PagesChangedEventArgs.cs
- ConfigurationStrings.cs
- UshortList2.cs
- PresentationTraceSources.cs
- SectionRecord.cs
- TextCompositionManager.cs
- WebPartPersonalization.cs
- MemoryMappedViewStream.cs
- ItemDragEvent.cs
- XmlAttributes.cs
- UpdatePanel.cs
- ScriptManager.cs
- SqlClientMetaDataCollectionNames.cs
- ComponentChangingEvent.cs
- cryptoapiTransform.cs
- UnionCqlBlock.cs
- Function.cs
- LogLogRecordHeader.cs
- RecordConverter.cs
- BaseTemplatedMobileComponentEditor.cs
- AnnotationResourceCollection.cs
- ObjectSelectorEditor.cs
- ToolStripItemClickedEventArgs.cs
- StrongName.cs
- Timer.cs
- ScriptManagerProxy.cs
- HttpClientChannel.cs
- PrtTicket_Base.cs
- MatrixAnimationUsingKeyFrames.cs
- EditorPartCollection.cs
- TextBox.cs
- UndoManager.cs
- DeviceFiltersSection.cs
- HebrewCalendar.cs
- TypeUsageBuilder.cs
- DataBoundControlActionList.cs
- DesigntimeLicenseContextSerializer.cs
- ComboBox.cs
- coordinatorscratchpad.cs
- oledbconnectionstring.cs
- SiteMapNodeCollection.cs
- WorkflowCreationContext.cs
- FixedDocumentPaginator.cs
- VirtualDirectoryMappingCollection.cs
- StructureChangedEventArgs.cs
- SqlRowUpdatingEvent.cs
- XPathConvert.cs
- Tracking.cs
- EntityDataSourceDesignerHelper.cs
- Point4DConverter.cs
- MenuItemBindingCollection.cs
- FlowDocumentScrollViewer.cs
- SearchForVirtualItemEventArgs.cs
- SignedInfo.cs
- WeakReferenceKey.cs
- ZoneButton.cs
- SQLConvert.cs
- DesignerDataTableBase.cs
- BlobPersonalizationState.cs
- SplitterPanel.cs
- FileRecordSequenceHelper.cs
- WeakEventTable.cs
- BitmapMetadataBlob.cs
- Annotation.cs
- LocatorBase.cs
- XmlSignatureManifest.cs
- Preprocessor.cs
- ClusterSafeNativeMethods.cs
- ListItem.cs
- AutomationEventArgs.cs
- TcpConnectionPoolSettingsElement.cs
- AesManaged.cs
- XmlSchemaValidationException.cs
- Root.cs
- FontEmbeddingManager.cs
- LinkedList.cs
- SmtpTransport.cs
- FactoryRecord.cs
- ParameterReplacerVisitor.cs
- TableColumnCollectionInternal.cs
- ColorConvertedBitmap.cs
- MsmqChannelFactory.cs
- XmlByteStreamWriter.cs
- GroupBoxRenderer.cs
- dbdatarecord.cs
- UnionExpr.cs
- EntityModelSchemaGenerator.cs
- FatalException.cs