如何从 SimpleMembership 迁移到 ASP.NET.Identity [英] How to migrate from SimpleMembership to ASP.NET.Identity
问题描述
从 MVC4 迁移到 MVC5 并且也想使用 ASP.NET 标识,但我正在努力寻找涵盖我认为迁移标识所需的所有内容的任何内容.将现有网站从 SimpleMembership 迁移到 ASP.NETIdentity 建议我需要做的就是创建一个 ApplicationUser
并迁移数据,其他网络搜索给我 sql 脚本和密码散列建议.但是在我开始之前,我还想清除其他一些未解决的问题.
Migrating from MVC4 to MVC5 and want to use ASP.NET Identity too but I am struggling to find anything that covers everything I think I need to do to migrate the Identity. Migrating an Existing Website from SimpleMembership to ASP.NET Identity suggests all I need to do is create an ApplicationUser
and migrate the data, and other web searches give me sql scripts and passowrd hashing advice. But there are other loose ends that I'd like to clear up before I jump in.
首先 - 我有代码在我的 Seed 方法中初始化会员系统:
First - I have code to initialise the membership system in my Seed method:
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection(Config.ConnectionStringName,
Config.UsersTableName,
Config.UsersIDColumn,
Config.UserNameColumn,
autoCreateTables: true);
所以需要正确吗?
第二 - 看起来我需要一个 IdentityDbContext
.那么我应该改变我现有的上下文来继承它吗?
Second - it looks like I need an IdentityDbContext
. So should I change my existing context to inherit that?
即而不是我当前的代码 public class SID2013Context : DbContext
这样做:public class SID2013Context : IdentityDbContext
i.e. instead of my current code public class SID2013Context : DbContext
do this: public class SID2013Context : IdentityDbContext<ApplicationUser>
这是否会生成一个迁移以创建我需要支持 ASP.NET Identity 的新表?
Will that generate a migration that creates the new tables I need to support ASP.NET Identity?
完成后,我应该能够引入 AccountController
、视图、视图模型并启动为 MVC5 项目生成的代码.
Once I've done that I should be able to pull in the AccountController
, Views, ViewModels and start up code that is generated for an MVC5 project.
谁能回答这里的具体问题和/或为我指出更好的资源?
Can anyone answer the specific questions here and/or point me to a better resource?
推荐答案
我意识到我在问题中链接的网站告诉您如何迁移到 Identity
表,但仍然使用 简单会员
.我想在整个过程中使用 Identity,所以我使用 Visual Studio 创建了一个空的 MVC 应用程序,以便我可以复制我想要的代码并修复它.以下是我让登录和注销工作所遵循的步骤:
I realised that the website I linked to in my question is telling you how to migrate to Identity
tables, but still use SimpleMembership
. I want to use Identity throughout, so I created an empty MVC application using Visual Studio so that I could copy in the code I wanted and fix it up. Here are the steps I followed to get Login and Logout working:
- 将
ApplicationUser.cs
添加到我的模型项目 - 将
IdentityConfig.cs
添加到我的 App_Start 文件夹 - 将
Startup.Auth.cs
添加到我的 App_Start 文件夹 - 将
AccountController.cs
添加到我的 Controllers 文件夹(重命名现有控制器) - 将
Startup.cs
添加到根文件夹 - 将
AccountViewModels.cs
添加到 ViewModels 文件夹 - 更改了我的上下文以继承
IdentityDbContext
(这确实意味着在迁移中创建了新表) - 添加了所需的 NuGet 包并修复了所有命名空间
- 项目现在构建
- 但是
Add-Migration
给出了一个错误......EntityType IdentityUserRole 没有定义键" - ....通过在我的覆盖中添加对
base.OnModelCreating
的调用解决了这个问题 添加迁移
&Update-Database
- 添加所需的表- 将我在
UserProfile
中的自定义字段添加到ApplicationUser
并更新数据库 - 使用 sql(在迁移中)将数据从旧表复制到新表.此处包含 Sql.NB 需要使用 Guid 填充
SecurityStamp
以防止登录时出错 - 登录和注销工作正常
- 删除了
UserProfile
、SimpleRoleProvider
和RoleManager
类 - 在必要时替换代码. - 删除了对
WebMatrix.Data
和WebMatrix.WebData
dll 的引用 - 从 web.config 中删除了
和
莉>
- Added
ApplicationUser.cs
to my Models project - Added
IdentityConfig.cs
to my App_Start folder - Added
Startup.Auth.cs
to my App_Start folder - Added
AccountController.cs
to my Controllers folder (renamed existing controller) - Added
Startup.cs
to root folder - Added
AccountViewModels.cs
to ViewModels folder - Changed my context to inherit
IdentityDbContext
(it does indeed mean that new tables are created in a migration) - Added the required NuGet packages and fixed up all the namespaces
- Project now builds
- But
Add-Migration
gave an error ... "EntityType IdentityUserRole has no key defined" - ....solved that by adding a call to
base.OnModelCreating
in my override Add-Migration
&Update-Database
- adds the tables required- Added my custom fields in
UserProfile
toApplicationUser
and updated the database - Used sql (in a migration) to copy data from old tables to new tables. Sql included here. NB need to populate
SecurityStamp
with a Guid to prevent error during Login - That's got Login and Logout working
- Removed
UserProfile
,SimpleRoleProvider
andRoleManager
classes - replacing the code where necessary. - Removed references to
WebMatrix.Data
andWebMatrix.WebData
dlls - Removed
<roleManager enabled="true" defaultProvider="simple">
and<membership defaultProvider="simple">
from web.config
步骤 14 中使用的 Sql:
Sql used in step 14:
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)
这篇关于如何从 SimpleMembership 迁移到 ASP.NET.Identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!