Asp.net App.Config中的System.Web RoleManager在运行时丢失或无效
我正在开发一个安装程序(EXE文件),它将部署和配置ASP.NET网站和相应的ASP.NET成员数据库 我有一个带有以下app.config的控制台应用程序,其中包括system.web部分:Asp.net App.Config中的System.Web RoleManager在运行时丢失或无效,asp.net,.net,.net-3.5,asp.net-membership,console-application,Asp.net,.net,.net 3.5,Asp.net Membership,Console Application,我正在开发一个安装程序(EXE文件),它将部署和配置ASP.NET网站和相应的ASP.NET成员数据库 我有一个带有以下app.config的控制台应用程序,其中包括system.web部分: <?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="LocalSqlServer1" connectionString="Serve
<?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部分无效或丢失一样。大多数情况下,我会收到下面的错误消息,有时会收到“角色未启用”
是否可以在控制台应用程序中使用ASP.NET成员身份和角色?并通过system.web部分以正常方式进行配置?如果是,怎么做?请注意,Visual Studio项目保存在网络共享上
根据请求更新-编译器设置
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:
更新-请注意,它是从网络共享运行的
该异常有一个内部异常,这是一个安全异常,因为该解决方案位于网络共享上,任何可以从网络共享提供运行解决方案的修复程序的人都将获得奖励
更新-请求的异常详细信息
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:
System.Configuration.ConfigurationErrorsException未处理
BareMessage=为system.web/roleManager创建配置节处理程序时出错:请求失败。
Filename=L:\InternetSharePointRepository\InternetSharePointSolution\InternetSharePointConsoleApplication1\bin\Debug\InternetSharePointConsoleApplication1.vshost.exe.Config
行=34
Message=为system.web/roleManager创建配置节处理程序时出错:请求失败。(L:\InternetSharePointRepository\InternetSharePointSolution\InternetSharePointConsoleApplication1\bin\Debug\InternetSharePointConsoleApplication1.vshost.exe.Config第34行)
Source=System.Web
堆栈跟踪:
在System.Web.Security.Roles.Initialize()中
在System.Web.Security.Roles.EnsureEnabled()中
位于System.Web.Security.Roles.CreateRole(字符串roleName)
在L:\InternetSharePointRepository\InternetSharePointSolution\InternetSharePointConsoleApplication1\Module1.vb中的InternetSharePointConsoleApplication1.Module1.Main()处:第7行
位于System.AppDomain.\u nexecutestAssembly(程序集,字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
InnerException:System.Security.SecurityException
GrantedSet=“”
消息=请求失败。
许可状态=
RefusedSet=“”
Source=mscorlib
Url=“”
堆栈跟踪:
在System.Reflection.MethodBase.PerformSecurityCheck(对象obj、RuntimeMethodHandle方法、IntPtr父对象、UInt32调用标志)
在System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr、Binder Binder、Object[]参数、CultureInfo区域性)
位于System.Configuration.TypeUtil.InvokeCortWithReflectionPermission(ConstructorInfo-ctor)
在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader)
在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions处(RuntimeConfigurationRecord configRecord、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader)
在System.Configuration.RuntimeConfigurationRecord.CreateSection(布尔输入、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader)
在System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader、字符串文件名、Int32行)
内部异常:
将TargetFramework更改为普通的.Net Framework 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>
非常感谢您确认会员资格和角色管理器部分在App.Config中有效,并且可以像在web应用程序中一样使用
但是,您可能会发现在调试或从网络位置运行时会引发异常—这是一种不寻常的情况,是我的问题的根本原因。我发现最好的解决方法是将构建输出路径更改为本地文件系统上的路径,如下图所示
希望这对其他人有帮助 我很确定这两件事我都做过,不过我会再检查一遍。例如,如果尚未引用system.web,它将不会编译,导入system.web.security和Roles.CreateRole时会出错?创建项目时,我将目标framework设置为.NET 3.5,因为它需要与SharePoint 2010 DLL接口。我在app.config中没有看到成员资格部分,你需要加上这个。该死!刚刚意识到这是因为解决方案在网络共享上,这导致了s