我正在开发一个安装程序(EXE文件),它将部署和配置ASP.NET网站和相应的ASP.NET成员资格数据库。
我有一个带有以下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做什么,我似乎都无法使用Roles或Membership。就像整个system.web部分无效或缺失一样。大多数情况下,我收到以下错误消息,有时我得到“角色未启用”。
是否可以在控制台应用中使用ASP.NET Membership和Roles?并通过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:
答案 0 :(得分:1)
将TargetFramework更改为正常的.Net Framework 4而不是Client。客户端不包括System.Web。如果不是问题,请将System.Web.dll添加到项目中作为参考。您可能需要浏览它。
此外,您需要将Membership部分添加到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>
答案 1 :(得分:1)
非常感谢TrevorBrooks确认Membership和RoleManager部分在App.Config中有效,并且可以像在Web应用程序中一样使用。
但是,您可能会发现在从网络位置调试或运行时会抛出异常 - 这是导致我出现问题的一个不寻常的情况。我找到的最佳解决方法是将构建输出路径更改为本地文件系统上的路径 - 如下图所示。
希望这有助于其他人!