0

我正在開發一個安裝程序(EXE文件),它將部署和配置ASP.NET網站和相應的ASP.NET成員數據庫。System.Web在App.Config中的RoleManager在運行時缺失或無效

我有以下的app.config一個控制檯應用程序,其中包括的System.Web部分:

<?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> 

不過,我似乎無法得到角色或成員的工作,不管是什麼我做app.config。就好像整個system.web部分無效或缺失一樣。大多數情況下,我收到下面的錯誤消息,有時會得到「角色未啓用」。

Error reading system.web roleManager

是否有可能利用ASP.NET成員資格和角色在一個控制檯應用程序?並通過system.web部分配置正常的方式?如果是這樣,怎麼樣?請注意,Visual Studio項目保存在網絡共享中。

更新 - 按要求編譯器設置

enter image description here

更新 - 請注意,它正在從網絡共享

異常的內例外,這是一個安全性異常運行,由於該解決方案是在網絡共享上引起的,任何能夠提供修補程序以從網絡共享中運行解決方案的人都將獲得賞金!

更新 - 異常詳細的要求

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

回答

1

非常感謝TrevorBrooks,以確認Membership和RoleManager部分在App.Config中可以工作,並且可以像在Web應用程序中一樣使用。

但是,您可能會發現在從網絡位置進行調試或運行時會引發異常 - 這是我的問題的根本原因。我發現的最佳解決方法是將構建輸出路徑更改爲本地文件系統上的路徑 - 如下圖中突出顯示的那樣。

enter image description here

希望這可以幫助其他人!

+0

這不是您原來的問題的答案:「是否可以在控制檯應用程序中使用ASP.NET成員資格和角色?並通過system.web部分配置常規方式?如果是這樣,怎麼辦?」 – TrevorBrooks

+0

確實如此,但它在「更新它從網絡共享運行」後不久用粗體顯示。堆棧溢出允許您編輯問題,因爲我現在已經這麼做了,以便更清楚。不要指望被我授予獎金的人投票!大多數人會很高興收到獎勵? –

+0

通常,兩個單獨的問題是兩個單獨的帖子。 – TrevorBrooks

1

更改TargetFramework正常.Net框架4,而不是客戶端。客戶端不包含System.Web。如果這不是問題,請將System.Web.dll添加到您的項目中作爲參考。您可能需要瀏覽它。

另外,你需要在會​​員節添加到System.Web程序,是這樣的:

<membership defaultProvider=」AspNetSqlProvider」> 
<providers> 
<clear/> 
<add name=」AspNetSqlProvider」 connectionStringName=」SqlConnection」 type=」System.Web.Security.SqlMembershipProvider」 
enablePasswordRetrieval=」false」 
enablePasswordReset=」true」 
requiresQuestionAndAnswer=」true」 
passwordFormat=」Hashed」 
applicationName=」TestApp」/> 
</providers> 
</membership> 

這裏是在你的配置整個System.Web程序的例子:

<system.web> 
    <roleManager enabled="true">  
     <providers>  
      <clear/>  
      <add name="AspNetSqlRoleProvider" connectionStringName="MyAspNetDB"  
       applicationName="/SampleRolesApp"  
       type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />  
     </providers>  
    </roleManager>  
    <membership>  
     <providers>  
      <clear/>  
      <add name="AspNetSqlMembershipProvider"  
       connectionStringName="MyAspNetDB"  
       enablePasswordRetrieval="false"  
       enablePasswordReset="true"  
       requiresQuestionAndAnswer="true"  
       applicationName="/SamplesRolesApp"  
       requiresUniqueEmail="false"  
       passwordFormat="Hashed"  
       maxInvalidPasswordAttempts="5"  
       minRequiredPasswordLength="7"  
       minRequiredNonalphanumericCharacters="1"  
       passwordAttemptWindow="10"  
       passwordStrengthRegularExpression=""       
       type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />  
     </providers>  
    </membership>    
    <authentication mode="Forms" /> 
    </system.web> 
+0

我很確定我已經完成了這兩件事,但我會仔細檢查。例如,如果系統不會編譯。網絡還沒有被引用,它會導入system.web.security和Roles.CreateRole錯誤? –

+0

當我創建項目時,我將目標框架設置爲.NET 3.5,因爲它需要與SharePoint 2010 dll連接。 –

+0

查看上面的更新。 –

相關問題