summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Messaging/Configuration/DotNetOpenAuthSection.cs
blob: e784c7e11500b830f5bd9a9a89647ccace1a9ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//-----------------------------------------------------------------------
// <copyright file="DotNetOpenAuthSection.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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
	/// this library's settings.
	/// </summary>
	[ContractVerification(true)]
	public class DotNetOpenAuthSection : ConfigurationSectionGroup {
		/// <summary>
		/// The name of the section under which this library's settings must be found.
		/// </summary>
		internal const string SectionName = "dotNetOpenAuth";

		/// <summary>
		/// The name of the &lt;openid&gt; sub-element.
		/// </summary>
		private const string OpenIdElementName = "openid";

		/// <summary>
		/// The name of the &lt;oauth&gt; sub-element.
		/// </summary>
		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);
				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);
			}
		}

		/// <summary>
		/// Gets the messaging configuration element.
		/// </summary>
		public static MessagingElement Messaging {
			get { return MessagingElement.Configuration; }
		}

		/// <summary>
		/// Gets the reporting configuration element.
		/// </summary>
		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];
		}
	}
}