Exception when specifying defaultProxy in app.conf

2019-04-05 17:30发布

问题:

We're seeing a very strange issue when running the following application from a network share under NET4.0. When specifying a defaultProxy section in app.config a System.Net.WebException is thrown. There is no problem when running from a local drive.

According to documentation applications will run as full-trust assemblies from a network share so we're assuming this should work just fine.

Any ideas how we can work around this problem?

Has anyone else experienced this issue or does anyone know why this might be happening?

Sample program

using System;
using System.Net;

namespace ProxyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                String s = new WebClient().DownloadString("http://www.google.com");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

app.config

  <?xml version="1.0"?>
  <configuration>
    <system.net>
      <defaultProxy useDefaultCredentials="true"/>
    </system.net>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
    </startup>
  </configuration>

Exception details

System.Net.WebException: An exception occurred during a WebClient request. ---> System.Configuration.ConfigurationErrorsException: Insufficient permissions for setting the configuration section 'defaultProxy'. ---> System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
   at System.Security.CodeAccessPermission.Demand()
   at System.Net.Configuration.DefaultProxySection.PostDeserialize()
   --- End of inner exception stack trace ---
   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Net.Configuration.DefaultProxySectionInternal.GetSection()
   at System.Net.WebRequest.get_InternalDefaultWebProxy()
   at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
   at System.Net.HttpRequestCreator.Create(Uri Uri)
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(Uri requestUri)
   at System.Net.WebClient.GetWebRequest(Uri address)
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at System.Net.WebClient.DownloadString(String address)
   at ProxyTest.Program.Main(String[] args) in Y:\Program.cs:line 12

回答1:

See this Microsoft Knowledge Base article:

Symptoms:

  • You run a Microsoft .NET Framework 4-based application that is stored on a network share.
  • The application calls a static method in the System.Configuration.ConfigurationManager class. For example, the application calls the ConfigurationManager.GetSection method.

In this scenario, a System.Security.SecurityException exception is thrown and then the application crashes.

Cause:

The issue occurs because the method fails to access configuration section from the application on network share.

You can request the hotfix from that site.



回答2:

We've seen similar symptoms - same error message

Insufficient permissions for setting the configuration section 'defaultProxy'

when running a Console application where the files were 'blocked'.

The files had been installed by downloading a zip file from a hyperlink and the zip file wasn't 'unblocked' before unzipping.

Unblocking all the files (you have to do them individually in Windows) from Explorer > right-click > Properties > Unblock resolved the problem.