Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Configuration / ConfigurationHelpers.cs / 1 / ConfigurationHelpers.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.ServiceModel.Configuration { using System; using System.Configuration; using System.Globalization; using System.Reflection; using System.Runtime.CompilerServices; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Diagnostics; using System.ServiceModel.Dispatcher; using System.ServiceModel.Description; using System.Web.Configuration; using System.Security.Permissions; using System.Security; internal static class ConfigurationHelpers { /// Be sure to update UnsafeGetAssociatedBindingCollectionElement if you modify this method internal static BindingCollectionElement GetAssociatedBindingCollectionElement(ContextInformation evaluationContext, string bindingCollectionName) { BindingCollectionElement retVal = null; BindingsSection bindingsSection = (BindingsSection)ConfigurationHelpers.GetAssociatedSection(evaluationContext, ConfigurationStrings.BindingsSectionGroupPath); if (null != bindingsSection) { try { retVal = bindingsSection[bindingCollectionName]; } catch (System.Collections.Generic.KeyNotFoundException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound, ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName)))); } catch (NullReferenceException) // System.Configuration.ConfigurationElement bug { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound, ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName)))); } } return retVal; } ////// Critical - uses SecurityCritical method UnsafeGetAssociatedSection which elevates /// /// Be sure to update GetAssociatedBindingCollectionElement if you modify this method [SecurityCritical] internal static BindingCollectionElement UnsafeGetAssociatedBindingCollectionElement(ContextInformation evaluationContext, string bindingCollectionName) { BindingCollectionElement retVal = null; BindingsSection bindingsSection = (BindingsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.BindingsSectionGroupPath); if (null != bindingsSection) { try { retVal = bindingsSection[bindingCollectionName]; } catch (System.Collections.Generic.KeyNotFoundException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound, ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName)))); } catch (NullReferenceException) // System.Configuration.ConfigurationElement bug { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound, ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName)))); } } return retVal; } /// Be sure to update UnsafeGetAssociatedSection if you modify this method internal static object GetAssociatedSection(ContextInformation evalContext, string sectionPath) { object retval = null; if (evalContext != null) { retval = evalContext.GetSection(sectionPath); } else { if (ServiceHostingEnvironment.IsHosted) { retval = GetSectionFromWebConfigurationManager(sectionPath); } else { retval = ConfigurationManager.GetSection(sectionPath); } // Trace after call to underlying configuration system to // insure that configuration system is initialized if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Verbose, TraceCode.GetConfigurationSection, new StringTraceRecord("ConfigurationSection", sectionPath), null, null); } } if (retval == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionNotFound, sectionPath))); } return retval; } ////// Critical - uses SecurityCritical methods which elevate /// /// Be sure to update GetAssociatedSection if you modify this method [SecurityCritical] internal static object UnsafeGetAssociatedSection(ContextInformation evalContext, string sectionPath) { object retval = null; if (evalContext != null) { retval = UnsafeGetSectionFromContext(evalContext, sectionPath); } else { if (ServiceHostingEnvironment.IsHosted) { if (ServiceHostingEnvironment.CurrentVirtualPath != null) { retval = UnsafeGetSectionFromWebConfigurationManager(sectionPath, ServiceHostingEnvironment.CurrentVirtualPath); } else { retval = UnsafeGetSectionFromWebConfigurationManager(sectionPath); } } else { retval = UnsafeGetSectionFromConfigurationManager(sectionPath); } // Trace after call to underlying configuration system to // insure that configuration system is initialized if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Verbose, TraceCode.GetConfigurationSection, new StringTraceRecord("ConfigurationSection", sectionPath), null, null); } } if (retval == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionNotFound, sectionPath))); } return retval; } /// Be sure to update UnsafeGetBindingCollectionElement if you modify this method internal static BindingCollectionElement GetBindingCollectionElement(string bindingCollectionName) { return GetAssociatedBindingCollectionElement(null, bindingCollectionName); } ////// Critical - uses SecurityCritical method UnsafeGetAssociatedBindingCollectionElement which elevates /// /// Be sure to update GetBindingCollectionElement if you modify this method [SecurityCritical] internal static BindingCollectionElement UnsafeGetBindingCollectionElement(string bindingCollectionName) { return UnsafeGetAssociatedBindingCollectionElement(null, bindingCollectionName); } internal static string GetBindingsSectionPath(string sectionName) { return string.Concat(ConfigurationStrings.BindingsSectionGroupPath, "/", sectionName); } /// Be sure to update UnsafeGetSection if you modify this method internal static object GetSection(string sectionPath) { return GetAssociatedSection(null, sectionPath); } ////// Critical - uses SecurityCritical method UnsafeGetAssociatedSection which elevates /// /// Be sure to update GetSection if you modify this method [SecurityCritical] internal static object UnsafeGetSection(string sectionPath) { return UnsafeGetAssociatedSection(null, sectionPath); } /// Be sure to update UnsafeGetSectionFromWebConfigurationManager if you modify this method [MethodImpl(MethodImplOptions.NoInlining)] static object GetSectionFromWebConfigurationManager(string sectionPath) { if (ServiceHostingEnvironment.CurrentVirtualPath != null) { return WebConfigurationManager.GetSection(sectionPath, ServiceHostingEnvironment.CurrentVirtualPath); } return WebConfigurationManager.GetSection(sectionPath); } ////// Critical - Asserts ConfigurationPermission in order to fetch config from WebConfigurationManager /// caller must guard return value /// /// Be sure to update GetSectionFromWebConfigurationManager if you modify this method [SecurityCritical] [ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)] [MethodImpl(MethodImplOptions.NoInlining)] internal static object UnsafeGetSectionFromWebConfigurationManager(string sectionPath) { return WebConfigurationManager.GetSection(sectionPath); } ////// Critical - Asserts ConfigurationPermission in order to fetch config from ConfigurationManager /// caller must guard return value /// [SecurityCritical] [ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)] static object UnsafeGetSectionFromConfigurationManager(string sectionPath) { return ConfigurationManager.GetSection(sectionPath); } ////// Critical - Asserts ConfigurationPermission in order to fetch config from ContextInformation /// caller must guard return value /// [SecurityCritical] [ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)] static object UnsafeGetSectionFromContext(ContextInformation evalContext, string sectionPath) { return evalContext.GetSection(sectionPath); } ////// Critical - Asserts ConfigurationPermission in order to fetch config from WebConfigurationManager /// caller must guard return value /// [SecurityCritical] [ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)] [MethodImpl(MethodImplOptions.NoInlining)] static object UnsafeGetSectionFromWebConfigurationManager(string sectionPath, string virtualPath) { return WebConfigurationManager.GetSection(sectionPath, virtualPath); } internal static string GetSectionPath(string sectionName) { return string.Concat(ConfigurationStrings.SectionGroupName, "/", sectionName); } ////// Critical - calls SetIsPresentWithAssert which elevates in order to set a property /// caller must guard ConfigurationElement parameter, ie only pass 'this' /// [SecurityCritical] internal static void SetIsPresent(ConfigurationElement element) { // Work around for VSW 578830: ConfigurationElements that override DeserializeElement cannot set ElementInformation.IsPresent PropertyInfo elementPresent = element.GetType().GetProperty("ElementPresent", BindingFlags.Instance | BindingFlags.NonPublic); SetIsPresentWithAssert(elementPresent, element, true); } ////// Critical - Asserts ReflectionPermission(MemberAccess) in order to set a private member in the ConfigurationElement /// caller must guard parameters /// [SecurityCritical] [ReflectionPermission(SecurityAction.Assert, MemberAccess=true)] static void SetIsPresentWithAssert(PropertyInfo elementPresent, ConfigurationElement element, bool value) { elementPresent.SetValue(element, value, null); } internal static ContextInformation GetEvaluationContext(IConfigurationContextProviderInternal provider) { if (provider != null) { try { return provider.GetEvaluationContext(); } catch (ConfigurationErrorsException) { } } return null; } ////// RequiresReview - the return value will be used for a security decision. if in doubt about the return value, it /// is safe to return null (caller will assume the worst case from a security perspective). /// internal static ContextInformation GetOriginalEvaluationContext(IConfigurationContextProviderInternal provider) { if (provider != null) { // provider may not need this try/catch, but it doesn't hurt to do it try { return provider.GetOriginalEvaluationContext(); } catch (ConfigurationErrorsException) { } } return null; } } interface IConfigurationContextProviderInternal { ////// return the current ContextInformation (the protected property ConfigurationElement.EvaluationContext) /// this may throw ConfigurationErrorsException, caller should guard (see ConfigurationHelpers.GetEvaluationContext) /// ///result of ConfigurationElement.EvaluationContext ContextInformation GetEvaluationContext(); ////// return the ContextInformation that was present when the ConfigurationElement was first deserialized. /// if Reset was called, this will be the value of parent.GetOriginalEvaluationContext() /// if Reset was not called, this will be the value of this.GetEvaluationContext() /// ///result of parent's ConfigurationElement.EvaluationContext ////// RequiresReview - the return value will be used for a security decision. if in doubt about the return value, it /// is safe (from a security perspective) to return null (caller will assume the worst case). /// ContextInformation GetOriginalEvaluationContext(); } ////// Critical -- stores information used in a security decision /// [SecurityCritical(SecurityCriticalScope.Everything)] struct EvaluationContextHelper { bool reset; ContextInformation inheritedContext; internal void OnReset(ConfigurationElement parent) { this.reset = true; this.inheritedContext = ConfigurationHelpers.GetOriginalEvaluationContext(parent as IConfigurationContextProviderInternal); } internal ContextInformation GetOriginalContext(IConfigurationContextProviderInternal owner) { if (this.reset) { // if reset, inherited context is authoritative, even if null return this.inheritedContext; } else { // otherwise use current return ConfigurationHelpers.GetEvaluationContext(owner); } } } } // 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
- EntityContainerEntitySet.cs
- UnmanagedMarshal.cs
- HttpContextWrapper.cs
- CodeBlockBuilder.cs
- SecurityKeyUsage.cs
- QueryReaderSettings.cs
- FixedPageAutomationPeer.cs
- ReferenceTypeElement.cs
- MarginCollapsingState.cs
- HttpAsyncResult.cs
- NumericExpr.cs
- SafeNativeMemoryHandle.cs
- WindowAutomationPeer.cs
- ChannelRequirements.cs
- SiteMapDataSourceView.cs
- TextRangeEditLists.cs
- PagePropertiesChangingEventArgs.cs
- ByteAnimation.cs
- NameNode.cs
- PagedControl.cs
- FormParameter.cs
- FloatSumAggregationOperator.cs
- InternalPermissions.cs
- BaseHashHelper.cs
- List.cs
- CompilerTypeWithParams.cs
- ResourcePool.cs
- SessionStateModule.cs
- PageWrapper.cs
- TextRange.cs
- DesignerSerializationOptionsAttribute.cs
- CodeSnippetCompileUnit.cs
- StorageEntitySetMapping.cs
- SQLUtility.cs
- StylusPointPropertyId.cs
- DiagnosticsConfigurationHandler.cs
- StyleBamlRecordReader.cs
- CustomTypeDescriptor.cs
- SafeHandles.cs
- CodeParameterDeclarationExpressionCollection.cs
- XmlSiteMapProvider.cs
- CleanUpVirtualizedItemEventArgs.cs
- LicenseProviderAttribute.cs
- SerializationEventsCache.cs
- LocalIdKeyIdentifierClause.cs
- RootAction.cs
- ListItemCollection.cs
- ObjectDataSourceChooseTypePanel.cs
- RefType.cs
- BindingExpression.cs
- Columns.cs
- FunctionParameter.cs
- ResourceExpressionEditorSheet.cs
- IdleTimeoutMonitor.cs
- Events.cs
- HostedNamedPipeTransportManager.cs
- XmlElementAttributes.cs
- SessionEndedEventArgs.cs
- Random.cs
- COM2AboutBoxPropertyDescriptor.cs
- XmlRawWriterWrapper.cs
- ModelPropertyImpl.cs
- AstNode.cs
- documentation.cs
- StickyNoteHelper.cs
- ASCIIEncoding.cs
- CurrentChangingEventArgs.cs
- DbConnectionPool.cs
- DifferencingCollection.cs
- Single.cs
- CodeIterationStatement.cs
- UTF32Encoding.cs
- SerializationStore.cs
- WebPartUserCapability.cs
- Rectangle.cs
- AuthenticationServiceManager.cs
- ParserOptions.cs
- uribuilder.cs
- Decoder.cs
- PageFunction.cs
- CodeComment.cs
- NameNode.cs
- UnsafeCollabNativeMethods.cs
- Converter.cs
- Pair.cs
- XamlStream.cs
- RadioButtonList.cs
- Matrix3D.cs
- SqlAliaser.cs
- LinqDataSourceEditData.cs
- QuotedPairReader.cs
- CommonObjectSecurity.cs
- ListViewInsertedEventArgs.cs
- WindowProviderWrapper.cs
- VSWCFServiceContractGenerator.cs
- DrawToolTipEventArgs.cs
- JsonDataContract.cs
- IntSecurity.cs
- ErrorsHelper.cs
- XPathDocumentBuilder.cs