1 //------------------------------------------------------------------------------
2 // <copyright file="DefaultProxySection.cs" company="Microsoft Corporation">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Net.Configuration
9 using System.Collections.Generic;
10 using System.Configuration;
11 using System.Globalization;
13 using System.Reflection;
14 using System.Threading;
15 using System.Security;
16 using System.Security.Permissions;
17 using System.Security.Principal;
18 using System.ComponentModel;
21 public sealed class DefaultProxySection : ConfigurationSection
23 public DefaultProxySection()
25 this.properties.Add(this.bypasslist);
26 this.properties.Add(this.module);
27 this.properties.Add(this.proxy);
28 this.properties.Add(this.enabled);
29 this.properties.Add(this.useDefaultCredentials);
32 protected override void PostDeserialize()
34 // Perf optimization. If the configuration is coming from machine.config
35 // It is safe and we don't need to check for permissions.
36 if (EvaluationContext.IsMachineLevel)
40 ExceptionHelper.WebPermissionUnrestricted.Demand();
41 } catch (Exception exception) {
42 throw new ConfigurationErrorsException(
43 SR.GetString(SR.net_config_section_permission,
44 ConfigurationStrings.DefaultProxySectionName),
49 [ConfigurationProperty(ConfigurationStrings.BypassList)]
50 public BypassElementCollection BypassList
52 get { return (BypassElementCollection)this[this.bypasslist]; }
55 [ConfigurationProperty(ConfigurationStrings.Module)]
56 public ModuleElement Module
58 get { return (ModuleElement)this[this.module]; }
61 protected override ConfigurationPropertyCollection Properties
63 get { return this.properties; }
66 [ConfigurationProperty(ConfigurationStrings.Proxy)]
67 public ProxyElement Proxy
69 get { return (ProxyElement)this[this.proxy]; }
72 [ConfigurationProperty(ConfigurationStrings.Enabled, DefaultValue = true)]
75 get { return (bool) this[this.enabled]; }
76 set { this[this.enabled] = value; }
79 [ConfigurationProperty(ConfigurationStrings.UseDefaultCredentials, DefaultValue = false)]
80 public bool UseDefaultCredentials
82 get { return (bool) this[this.useDefaultCredentials]; }
83 set { this[this.useDefaultCredentials] = value; }
86 ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
88 readonly ConfigurationProperty bypasslist =
89 new ConfigurationProperty(ConfigurationStrings.BypassList,
90 typeof(BypassElementCollection),
92 ConfigurationPropertyOptions.None);
94 readonly ConfigurationProperty module =
95 new ConfigurationProperty(ConfigurationStrings.Module,
96 typeof(ModuleElement),
98 ConfigurationPropertyOptions.None);
100 readonly ConfigurationProperty proxy =
101 new ConfigurationProperty(ConfigurationStrings.Proxy,
102 typeof(ProxyElement),
104 ConfigurationPropertyOptions.None);
106 readonly ConfigurationProperty enabled =
107 new ConfigurationProperty(ConfigurationStrings.Enabled,
110 ConfigurationPropertyOptions.None);
112 readonly ConfigurationProperty useDefaultCredentials =
113 new ConfigurationProperty(ConfigurationStrings.UseDefaultCredentials,
116 ConfigurationPropertyOptions.None);
119 // This allows us to prevent parent settings (machine.config) from propegating to higher config (app.config), unless
120 // the higher config doesn't contain the section at all. That is, overriding defaultProxy is all-or-nothing.
121 // Template from [....].
122 protected override void Reset(ConfigurationElement parentElement)
124 // Ignore the parentElement parameter by changing it to the default settings
125 DefaultProxySection defaultElement = new DefaultProxySection();
127 // Initialize the parentElement to the right set of defaults (not needed now,
128 // but this will avoid errors in the future if SetDefaults is ever overridden in this class.
129 // ConfigurationElement::InitializeDefault is a no-op, so you aren
\92t hurting perf by anything
131 defaultElement.InitializeDefault();
133 // Finally, pass it to the base class to do the
\93right things
\94
134 base.Reset(defaultElement);
138 internal sealed class DefaultProxySectionInternal
140 [SecurityPermission(SecurityAction.Assert, Flags=SecurityPermissionFlag.ControlPrincipal)]
141 internal DefaultProxySectionInternal(DefaultProxySection section)
143 // If enabled is false, skip everything.
144 if (!section.Enabled)
149 // If nothing else is set, use the system default.
150 if (section.Proxy.AutoDetect == ProxyElement.AutoDetectValues.Unspecified &&
151 section.Proxy.ScriptLocation == null &&
152 String.IsNullOrEmpty(section.Module.Type) &&
153 section.Proxy.UseSystemDefault != ProxyElement.UseSystemDefaultValues.True &&
154 section.Proxy.ProxyAddress == null &&
155 section.Proxy.BypassOnLocal == ProxyElement.BypassOnLocalValues.Unspecified &&
156 section.BypassList.Count == 0)
158 // Old-style indication to turn off the proxy.
159 if (section.Proxy.UseSystemDefault == ProxyElement.UseSystemDefaultValues.False)
161 this.webProxy = new EmptyWebProxy();
163 // Intentionally ignoring UseDefaultCredentials in this case.
167 // Suspend impersonation.
169 new SecurityPermission(SecurityPermissionFlag.ControlPrincipal | SecurityPermissionFlag.UnmanagedCode).Assert();
171 using(WindowsIdentity.Impersonate(IntPtr.Zero))
172 #endif // !FEATURE_PAL
174 CodeAccessPermission.RevertAssert();
175 this.webProxy = new WebRequest.WebProxyWrapper(new WebProxy(true));
183 // First, check out if we are using a different module type
184 if (!String.IsNullOrEmpty(section.Module.Type))
186 Type theType = Type.GetType(section.Module.Type, true, true);
188 if ((theType.Attributes & TypeAttributes.VisibilityMask) != TypeAttributes.Public)
189 throw new ConfigurationErrorsException(SR.GetString(SR.net_config_proxy_module_not_public));
191 // verify that its of the proper type of IWebProxy
192 if (!typeof(IWebProxy).IsAssignableFrom(theType))
194 throw new InvalidCastException(SR.GetString(SR.net_invalid_cast,
198 this.webProxy = (IWebProxy)Activator.CreateInstance(
200 BindingFlags.CreateInstance
201 | BindingFlags.Instance
202 | BindingFlags.NonPublic
203 | BindingFlags.Public,
205 new object[0], // no arguments
206 CultureInfo.InvariantCulture
209 else if (section.Proxy.UseSystemDefault == ProxyElement.UseSystemDefaultValues.True &&
210 section.Proxy.AutoDetect == ProxyElement.AutoDetectValues.Unspecified &&
211 section.Proxy.ScriptLocation == null)
213 // Suspend impersonation. This setting is deprecated but required for Everett compat.
215 new SecurityPermission(SecurityPermissionFlag.ControlPrincipal | SecurityPermissionFlag.UnmanagedCode).Assert();
217 using(WindowsIdentity.Impersonate(IntPtr.Zero))
218 #endif // !FEATURE_PAL
220 CodeAccessPermission.RevertAssert();
221 this.webProxy = new WebProxy(false);
229 this.webProxy = new WebProxy();
232 WebProxy tempProxy = this.webProxy as WebProxy;
234 if (tempProxy != null)
236 if (section.Proxy.AutoDetect != ProxyElement.AutoDetectValues.Unspecified)
238 tempProxy.AutoDetect = section.Proxy.AutoDetect == ProxyElement.AutoDetectValues.True;
240 if (section.Proxy.ScriptLocation != null)
242 tempProxy.ScriptLocation = section.Proxy.ScriptLocation;
244 if (section.Proxy.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified)
246 tempProxy.BypassProxyOnLocal = section.Proxy.BypassOnLocal == ProxyElement.BypassOnLocalValues.True;
248 if (section.Proxy.ProxyAddress != null)
250 tempProxy.Address = section.Proxy.ProxyAddress;
252 int bypassListSize = section.BypassList.Count;
253 if (bypassListSize > 0)
255 string[] bypassList = new string[section.BypassList.Count];
256 for (int index = 0; index < bypassListSize; ++index)
258 bypassList[index] = section.BypassList[index].Address;
260 tempProxy.BypassList = bypassList;
263 // Wrap it if type not explicitly specified in Module.
264 if (section.Module.Type == null)
266 this.webProxy = new WebRequest.WebProxyWrapper(tempProxy);
271 // Now apply UseDefaultCredentials if there's a proxy.
272 if (this.webProxy != null && section.UseDefaultCredentials)
274 this.webProxy.Credentials = SystemNetworkCredential.defaultCredential;
278 internal static object ClassSyncObject
282 if (classSyncObject == null)
284 object o = new object();
285 Interlocked.CompareExchange(ref classSyncObject, o, null);
287 return classSyncObject;
291 static internal DefaultProxySectionInternal GetSection()
293 lock (DefaultProxySectionInternal.ClassSyncObject)
295 DefaultProxySection section = PrivilegedConfigurationManager.GetSection(ConfigurationStrings.DefaultProxySectionPath) as DefaultProxySection;
301 return new DefaultProxySectionInternal(section);
303 catch (Exception exception)
305 if (NclUtilities.IsFatal(exception)) throw;
307 throw new ConfigurationErrorsException(SR.GetString(SR.net_config_proxy), exception);
312 internal IWebProxy WebProxy
314 get { return this.webProxy; }
317 private IWebProxy webProxy;
318 private static object classSyncObject;