Configuring MVC for SimpleMembership

依然范特西╮ 提交于 2020-01-26 01:35:33

问题


I have an ASP.NET MVC application with a number of membership-related pages generated from the project templates.

When I attempt to access one of those pages, I get the following error:

The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588.

After some time spent researching, I determined I was missing the connection string. I have an existing connection string named FreeWebFilesEntities and I created a new connection string named DefaultConnection, and gave it the same value.

<connectionStrings>
    <add name="FreeWebFilesEntities" connectionString="metadata=res://*/FreeWebFilesRepository.csdl|res://*/FreeWebFilesRepository.ssdl|res://*/FreeWebFilesRepository.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=FreeWebFiles;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection" connectionString="metadata=res://*/FreeWebFilesRepository.csdl|res://*/FreeWebFilesRepository.ssdl|res://*/FreeWebFilesRepository.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=FreeWebFiles;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
<connectionStrings>

But it still doesn't like this. I found some resources that suggested I'm using an EF connection string (and I am). I tried removing the metadata section of it but, no matter what I do, it throws an exception.

Since it appears Microsoft decided not to document this process very carefully (if at all), has anyone figured this out? How can I get the SimpleMembership pages to work?


回答1:


Well, for starters, you are using a EntityClient connection string, which is incompatible with SimpleMembership. SimpleMembership requires a SqlClient provider type connection string (ie, it does not have the metadata and the provider type is SqlClient)

Second, the connection string that is used is set in your InitializeSimpleMembershipAttribute.cs class, in particular the line that calls WebSecurity.InitializeDatabaseConnection. You can change this to whatever you want. DefaultConnection is the default connection string supplied in the machine.config, and does not by default show up in your web.config.




回答2:


Do you have these sections in your web.config file?

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


来源:https://stackoverflow.com/questions/17266322/configuring-mvc-for-simplemembership

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!