diff options
Diffstat (limited to 'src/DotNetOpenAuth.Messaging/Configuration/DotNetOpenAuthSection.cs')
-rw-r--r-- | src/DotNetOpenAuth.Messaging/Configuration/DotNetOpenAuthSection.cs | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/DotNetOpenAuth.Messaging/Configuration/DotNetOpenAuthSection.cs b/src/DotNetOpenAuth.Messaging/Configuration/DotNetOpenAuthSection.cs index 87e335c..e784c7e 100644 --- a/src/DotNetOpenAuth.Messaging/Configuration/DotNetOpenAuthSection.cs +++ b/src/DotNetOpenAuth.Messaging/Configuration/DotNetOpenAuthSection.cs @@ -5,8 +5,11 @@ //----------------------------------------------------------------------- namespace DotNetOpenAuth.Configuration { + using System; using System.Configuration; using System.Diagnostics.Contracts; + using System.Web; + using System.Web.Configuration; /// <summary> /// Represents the section in the host's .config file that configures @@ -30,19 +33,39 @@ namespace DotNetOpenAuth.Configuration { private const string OAuthElementName = "oauth"; /// <summary> + /// A value indicating whether this instance came from a real Configuration instance. + /// </summary> + private bool synthesizedInstance; + + /// <summary> /// Initializes a new instance of the <see cref="DotNetOpenAuthSection"/> class. /// </summary> internal DotNetOpenAuthSection() { } /// <summary> + /// Initializes a new instance of the <see cref="DotNetOpenAuthSection"/> class. + /// </summary> + private DotNetOpenAuthSection(bool synthesized) { + this.synthesizedInstance = synthesized; + } + + /// <summary> /// Gets the configuration section from the .config file. /// </summary> public static DotNetOpenAuthSection Configuration { get { Contract.Ensures(Contract.Result<DotNetOpenAuthSection>() != null); - var configuration = ConfigurationManager.OpenExeConfiguration(null); - return (DotNetOpenAuthSection)configuration.GetSectionGroup(SectionName); + Configuration configuration; + if (HttpContext.Current != null) { + configuration = HttpContext.Current.Request.ApplicationPath != null + ? WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath) + : ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + } else { + configuration = ConfigurationManager.OpenExeConfiguration(null); + } + + return (DotNetOpenAuthSection)configuration.GetSectionGroup(SectionName) ?? new DotNetOpenAuthSection(true); } } @@ -59,5 +82,12 @@ namespace DotNetOpenAuth.Configuration { internal static ReportingElement Reporting { get { return ReportingElement.Configuration; } } + + /// <summary> + /// Gets a named section in this section group, or <c>null</c> if no such section is defined. + /// </summary> + internal ConfigurationSection GetNamedSection(string name) { + return this.synthesizedInstance ? null : this.Sections[name]; + } } } |