从 MVC4 迁移到 MVC5,也想使用 ASP.NET Identity,但我很难找到涵盖我认为迁移 Identity 所需的所有内容的内容。 Migrating an Existing Website from SimpleMembership to ASP.NET Identity建议我需要做的就是创建一个 ApplicationUser
并迁移数据,其他网络搜索为我提供了 SQL 脚本和密码哈希建议。但在开始之前,我还想解决一些其他 Unresolved 问题。
首先 - 我有代码在我的种子方法中初始化成员(member)系统:
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection(Config.ConnectionStringName,
Config.UsersTableName,
Config.UsersIDColumn,
Config.UserNameColumn,
autoCreateTables: true);
所以这需要正确吗?
第二 - 看起来我需要一个 IdentityDbContext
。那么我应该改变现有的上下文来继承它吗?
即而不是我当前的代码 public class SID2013Context : DbContext
这样做:public class SID2013Context : IdentityDbContext<ApplicationUser>
这会生成一个迁移来创建我需要支持 ASP.NET Identity 的新表吗?
完成后,我应该能够拉入 AccountController
、 View 、ViewModel 和为 MVC5 项目生成的启动代码。
任何人都可以回答此处的具体问题和/或为我指出更好的资源吗?
最佳答案
我意识到我在问题中链接到的网站正在告诉您如何迁移到 Identity
表,但仍使用SimpleMembership
。我想在整个过程中使用 Identity,因此我使用 Visual Studio 创建了一个空的 MVC 应用程序,以便我可以复制我想要的代码并修复它。以下是我为登录和注销工作所遵循的步骤:
- 已添加
ApplicationUser.cs
到我的模型项目 - 已添加
IdentityConfig.cs
到我的 App_Start 文件夹 - 已添加
Startup.Auth.cs
到我的 App_Start 文件夹 - 已添加
AccountController.cs
到我的 Controllers 文件夹(重命名为现有 Controller ) - 已添加
Startup.cs
到根文件夹 - 已添加
AccountViewModels.cs
到 ViewModels 文件夹 - 更改了我的上下文以继承
IdentityDbContext
(这确实意味着在迁移中创建了新表) - 添加了所需的 NuGet 包并修复了所有命名空间
- 项目现已构建
- 但是
Add-Migration
出现错误...“EntityType IdentityUserRole 没有定义键” - ...通过添加对
base.OnModelCreating
的调用解决了这个问题在我的覆盖中 -
Add-Migration
&Update-Database
- 添加所需的表格 - 在
UserProfile
中添加了我的自定义字段至ApplicationUser
并更新了数据库 - (在迁移中)使用 SQL 将数据从旧表复制到新表。 SQL包含在这里。注意需要填写
SecurityStamp
使用 Guid 防止登录时出错 - 登录和注销就可以正常工作了
- 已删除
UserProfile
,SimpleRoleProvider
和RoleManager
类 - 在必要时替换代码。 - 删除了对
WebMatrix.Data
的引用和WebMatrix.WebData
dll - 已删除
<roleManager enabled="true" defaultProvider="simple">
和<membership defaultProvider="simple">
来自 web.config
第14步中使用的Sql:
INSERT INTO dbo.aspnetusers (id
, email
, emailconfirmed
, passwordhash
, securitystamp
, phonenumber
, phonenumberconfirmed
, twofactorenabled
, lockoutenddateutc
, lockoutenabled
, accessfailedcount
, username
, organisationid
, firstname
, lastname
, inactive)
SELECT
u.id,
u.username Email,
m.isconfirmed EmailConfirmed,
m.password PasswordHash,
--SignInManager.PasswordSignInAsync (used in Login method)
--throws an exception http://stackoverflow.com/a/23354148/150342
NEWID() SecurityStamp,
u.telephone PhoneNumber,
CASE
WHEN u.telephone IS NULL THEN 0
ELSE 1
END PhoneNumberConfirmed,
0 TwoFactorEnabled,
NULL LockoutEndDateUtc,
0 LockoutEnabled,
m.passwordfailuressincelastsuccess AccessFailedCount,
u.username,
u.organisationid,
u.firstname,
u.lastname,
u.inactive
FROM dbo.userprofiles u
INNER JOIN dbo.webpages_membership m
ON m.userid = u.id
WHERE NOT EXISTS (SELECT
1
FROM dbo.aspnetusers
WHERE id = u.id)
INSERT INTO dbo.aspnetroles (id
, name)
SELECT
roleid,
rolename
FROM dbo.webpages_roles r
WHERE NOT EXISTS (SELECT
1
FROM dbo.aspnetroles
WHERE roleid = r.roleid)
INSERT INTO dbo.aspnetuserroles (userid
, roleid)
SELECT
userid,
roleid
FROM dbo.webpages_usersinroles ur
WHERE NOT EXISTS (SELECT
1
FROM dbo.aspnetuserroles
WHERE userid = ur.userid
AND roleid = ur.roleid)
关于asp.net-mvc-4 - 如何从 SimpleMembership 迁移到 ASP.NET.Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533784/