我的任务是从我们旧的半生不熟的用户存储(使用加密密码)迁移到默认的 MS 成员资格/角色提供程序。我在其他项目中做过这个没问题。
在这种情况下,我使用“新的”.NET 通用提供程序使用代码优先创建了必要的表。我只是想将新记录插入到用户表中,然后撞到了墙上。
我有一个简单的 edmx,其中包含由会员提供者创建的 6 个表。我已将 web.config 设置为指向必要的 cxn 字符串和成员资格/角色提供程序(代码如下)。
网络配置:
<connectionStrings>
<add name="Test" connectionString="metadata=res://*/Entities.Test.csdl|res://*/Entities.Test.ssdl|res://*/Entities.Test.msl;provider=System.Data.SqlClient;provider connection string="data source=[OurServer];initial catalog=[OurDb];integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider" connectionStringName="Test" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
<providers>
<add connectionStringName="Test" applicationName="/" name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider" />
</providers>
</roleManager>
这是我的“更新密码”功能:
System.Web.Security.MembershipCreateStatus status = new System.Web.Security.MembershipCreateStatus();
//Get list of existing users, containing Username & encrypted PW
List<UserMigration.Models.Users> usersList = UserMigration.Models.Users.GetUsers();
foreach (var user in usersList)
{
string password = "";
//We have two encryption methods (bogus I know), so I attempt to decrypt with one, then another, using existing utility functions.
if (!EncryptDecrypt.TryDecryptTripleDES(user.Password, out password))
{
EncryptDecrypt.TryDecrypt(user.Password, out password);
}
//if we got a password, lets party
if (!string.IsNullOrEmpty(password))
{
System.Web.Security.Membership.CreateUser(user.Username, password,string.Empty,null,null,true,out status);
}
}
我在 CreateUser 调用中遇到错误,我终生无法弄清楚原因。我没有以任何方式修改实体。这应该很简单。错误是:
Schema specified is not valid. Errors:
The relationship 'TestModel.MembershipEntity_Application' was not loaded because the type 'TestModel.Membership' is not available.
The relationship 'TestModel.RoleEntity_Application' was not loaded because the type 'TestModel.Role' is not available.
The relationship 'TestModel.User_Application' was not loaded because the type 'TestModel.User' is not available.
The following information may be useful in resolving the previous error:
The required property 'Roles' does not exist on the type 'System.Web.Providers.Entities.User'.