Search code examples
asp.net.net.net-3.5asp.net-membershipconsole-application

System.Web RoleManager in App.Config is missing or invalid during run-time


I am developing a setup program (EXE file) which will deploy and configure an ASP.NET website and corresponding ASP.NET membership database.

I have a console app with the following app.config, which includes a system.web section:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="LocalSqlServer1" connectionString="Server=.\NAME;Database=aspnetdb;Integrated Security=true" />
  </connectionStrings>
  <system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="FileLog" />
          <!-- Uncomment the below section to write to the Application Event Log -->
          <!--<add name="EventLog"/>-->
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" />
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    </sharedListeners>
  </system.diagnostics>
  <system.web>
    <roleManager defaultProvider="SqlProvider1"
      enabled="true"
      cacheRolesInCookie="true"
      cookieName=".ASPROLES"
      cookieTimeout="30"
      cookiePath="/"
      cookieRequireSSL="false"
      cookieSlidingExpiration="true"
      cookieProtection="All" >
      <providers>
        <add
          name="SqlProvider1"
          type="System.Web.Security.SqlRoleProvider"
          connectionStringName="LocalSqlServer1"
          applicationName="SampleApplication" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

However, I can't seem to get Roles or Membership to work, no matter what I do to app.config. It's as if the whole system.web section is invalid or missing. Mostly I am getting the below error message, sometimes I get "Roles are not enabled".

Error reading system.web roleManager

Is it possible to utilize ASP.NET Membership and Roles in a console app? and configured the normal way via system.web section? If so, how? Please note the Visual Studio project is saved on a network share.

Update - Compiler Settings as requested

enter image description here

Update - Please note it is being run from a network share

The exception has an inner exception which is a security exception, caused because the solution is on a network share, anyone who can provide a fix for running solutions from network share will get the bounty!

Update - Exception details as requested

System.Configuration.ConfigurationErrorsException was unhandled
  BareMessage=An error occurred creating the configuration section handler for system.web/roleManager: Request failed.
  Filename=L:\InternetSharePointRepository\InternetSharePointSolution\InternetSharePointConsoleApplication1\bin\Debug\InternetSharePointConsoleApplication1.vshost.exe.Config
  Line=34
  Message=An error occurred creating the configuration section handler for system.web/roleManager: Request failed. (L:\InternetSharePointRepository\InternetSharePointSolution\InternetSharePointConsoleApplication1\bin\Debug\InternetSharePointConsoleApplication1.vshost.exe.Config line 34)
  Source=System.Web
  StackTrace:
       at System.Web.Security.Roles.Initialize()
       at System.Web.Security.Roles.EnsureEnabled()
       at System.Web.Security.Roles.CreateRole(String roleName)
       at InternetSharePointConsoleApplication1.Module1.Main() in L:\InternetSharePointRepository\InternetSharePointSolution\InternetSharePointConsoleApplication1\Module1.vb:line 7
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Security.SecurityException
       GrantedSet=""
       Message=Request failed.
       PermissionState=<PermissionSet class="System.Security.PermissionSet"
version="1">
<IPermission class="System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Level="Minimal"/>
</PermissionSet>

       RefusedSet=""
       Source=mscorlib
       Url=""
       StackTrace:
            at System.Reflection.MethodBase.PerformSecurityCheck(Object obj, RuntimeMethodHandle method, IntPtr parent, UInt32 invocationFlags)
            at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
            at System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(ConstructorInfo ctor)
            at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
            at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
            at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
            at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)
       InnerException: 

enter image description here


Solution

  • Many thanks to TrevorBrooks for confirming that Membership and RoleManager sections do work in App.Config and can be used just like in a web application.

    However, you may find that exceptions are thrown when debugging or running from a network location - an unusual scenario that was the root cause of my problem. The best workaround I have found is to change the build output path to a path on the local file system - as highlighted in the image below.

    enter image description here

    Hope this helps someone else!