0

我正在尝试使用以下建议配置自定义成员资格提供程序: Custom MembershipProvider in .NET 4.0

我的环境:VS2012,ASP.NET NVC4 Internet Application(由向导创建)。我的目标是管理外部数据存储中的用户。

添加提供程序代码后...我将以下部分添加到 web.config 部分:

 <membership defaultProvider="MyMembershipProvider">
  <providers>
    <clear/>
    <add name="MyMembershipProvider" type=" MvcApplication2.Models.MyMembershipProvider" connectionStringName="DefaultConnection"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>

尝试调试应用程序后,我收到以下错误:

Server Error in '/' Application.

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: This method cannot be called during the application's pre-start initialization phase.

Source Error: 


Line 41:       <providers>
Line 42:         <clear/>
Line 43:         <add name="MyMembershipProvider" type="MvcApplication2.Models.MyMembershipProvider" connectionStringName="DefaultConnection"

我尝试遵循这篇文章的建议: ASP.NET:在应用程序的预启动初始化阶段无法调用此方法

不幸的是,在按照描述添加设置后:

 <add key="enableSimpleMembership" value="false"/>

该应用程序出现,但在按下登录后,我在 Visual Studio 中收到以下异常:

System.Reflection.TargetInvocationException was unhandled by user code
  HResult=-2146232828
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
           at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at System.Threading.LazyHelpers`1.ActivatorFactorySelector()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock)
   at MvcApplication5.Filters.InitializeSimpleMembershipAttribute.OnActionExecuting(ActionExecutingContext filterContext) in c:\Users\max\Documents\Visual Studio 2012\Projects\MvcApplication2\MvcApplication5\Filters\InitializeSimpleMembershipAttribute.cs:line 21
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<>c__DisplayClass3b.<BeginInvokeActionMethodWithFilters>b__35()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain)
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588
       Source=MvcApplication5
   StackTrace:
        at MvcApplication5.Filters.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in c:\Users\max\Documents\Visual Studio 2012\Projects\MvcApplication2\MvcApplication5\Filters\InitializeSimpleMembershipAttribute.cs:line 45
   InnerException: System.Configuration.Provider.ProviderException
        HResult=-2146233088
        Message=The Role Manager feature has not been enabled.
        Source=System.Web
        StackTrace:
             at System.Web.Security.Roles.EnsureEnabled()
             at System.Web.Security.Roles.get_Provider()
             at WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables)
             at WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String connectionStringName, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables)
             at MvcApplication5.Filters.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in c:\Users\max\Documents\Visual Studio 2012\Projects\MvcApplication2\MvcApplication5\Filters\InitializeSimpleMembershipAttribute.cs:line 41
        InnerException: 

有什么问题?感谢您的任何提示,

最大限度

4

1 回答 1

2

这里可能有几个项目错误。首先,您对成员资格提供程序的配置似乎不适合 type 属性。你有:

type="MvcApplication2.Models.MyMembershipProvider"

该类型还应该包括这样的程序集名称:

type="MvcApplication2.Models.MyMembershipProvider, MvcApplication2"

假设 MvcApplication2 是包含您的提供程序的程序集的名称。

此外,插入到 MVC 4 Internet 模板的基本代码中的 SimpleMembership 使用了一些惰性初始化,从您的错误来看,它正在被调用。请务必从 AccountController中删除InitializeSimpleMembership属性。

[Authorize]
[InitializeSimpleMembership] //Remove this attribute
public class AccountController : Controller
{

这会导致 SimpleMembership 在您访问此控制器上的操作时进行初始化。您可以在此处阅读有关删除此初始化的更多信息。本文还讨论了如何自定义 SimpleMembership。您可能希望查看该选项,而不是从头开始创建自己的会员提供程序。

如果您想将 SimpleMembership 与您自己的提供程序一起使用,请从SimpleMembershipProvider继承并在 web.config 中进行配置。

<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <clear/>
    <add name="MySimpleMembershipProvider" type="MyNamespace.MySimpleMembershipProvider, MyAssembly" />
  </providers>
</membership>

如果需要初始化数据库,可以在 Global.asax Application_Start 方法中进行。

另一个问题可能是您没有配置角色提供程序。您是创建了自己的自定义角色提供程序还是尝试使用 SimpleMembership 角色提供程序。如果您不使用角色提供程序,请从 web.config 中删除此条目。

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
  <providers>
    <clear/>
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
  </providers>
</roleManager>

并将其添加到您的 web.config

<system.webServer>
  ...
  <modules runAllManagedModulesForAllRequests="true">
    <!-- This makes sure the ASP.NET security pipeline does not use any role provider.-->
    <remove name="RoleManager" />
  </modules>
</system.webServer>
于 2013-09-09T14:45:38.157 回答