我有一个与webforms一起使用的现有SQL成员资格数据库,我正在尝试将其设置为使用mvc4但没有运气,当我尝试通过id获取用户时(我知道此用户存在)我得到null异常。网络我打开网络应用程序配置我可以清楚地看到它没有成员或角色..等等。
以下是来自我的网站froms app的配置的一部分:
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=myserver;Initial Catalog=mydb;User ID=myid;Password=mypwd" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="UrgentPoints" type="System.Int32" defaultValue="0" />
</properties>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
....
这是mvc:
<configuration>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=myserver;Initial Catalog=mydb;User ID=myid;Password=mypwd" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="true" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership>
<providers>
<add connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="false"
requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</membership>
<roleManager>
<providers>
<add connectionStringName="DefaultConnection" applicationName="/"
name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</roleManager>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</sessionState>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
答案 0 :(得分:22)
这是我用谷歌搜索3天后的方式。使用MVC4 RC,.net Framework 4.5,SQL Server Express与.NET 4创建的新SqlMemberDB
会导致旧的SqlMemberDB丢失一些存储过程。
将以下设置添加到<AppSetting>
,如下所示,以禁用“简单会员”:
<add key="enableSimpleMembership" value="false" />
根据需要添加数据库连接。我正在使用SQL Server 2000,因此禁用用户实例。
<add name="ScqMember_Context"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MemberDB;User ID=xxx;Password=xxx;Integrated Security=False;User Instance=False;MultipleActiveResultSets=False"
providerName="System.Data.SqlClient" />
在v4 .NET framework下从machine.ini复制设置配置。
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ScqMember_Context"
enablePasswordRetrieval="false" enablePasswordReset="true"
requiresQuestionAndAnswer="true" applicationName="/"
requiresUniqueEmail="false" passwordFormat="Hashed"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7"
minRequiredNonalphanumericCharacters="1"
passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider"
connectionStringName="ScqMember_Context"
applicationName="/"
type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ScqMember_Context"
applicationName="/"
name="AspNetSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
从帐户控制器更改登录(),如下所示
/// aspnetSQLMember Authentication
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
从AccountController更新Logoff模块,如下所示
/// aspnetSQLMember Authentication
FormsAuthentication.SignOut();
谢谢,
答案 1 :(得分:4)
首先确保切换到Asp.Net Universal Providers(nuget包),然后确保连接到与旧应用程序相同的DB。 asp.net数据库的模式没有改变,应该可以与MVC 4一起使用。
答案 2 :(得分:1)
我遇到了同样的问题 - 将MVC4应用程序指向“旧的”aspnet数据库。无法在“默认”设置下检索现有用户/密码(System.Web.Providers.DefaultProfileProvider) - &gt;请注意,这些密码已加密,并且适当的设置存在于本地web.config(在system.web下)
<machineKey
validationKey= "A3..."
decryptionKey= "8B..."
validation="SHA1"
decryption="AES"/>
如果我添加一个具有“DefaultProfileProvider”的新用户,可以对用户进行协调和身份验证 - 它只适用于添加了“旧”提供程序的现有用户。
我没有机会“切换”到旧的,但我怀疑它有效(感谢user1010609) @Israel - 它很奇怪它应该有用 - 但它没有;)
答案 3 :(得分:1)
请记住,您必须在项目的引用中包含库: 的 System.Web.Providers.dll 强>