如何从迁移到SimpleMembership ASP.NET.Identity [英] How to migrate from SimpleMembership to ASP.NET.Identity

查看:261
本文介绍了如何从迁移到SimpleMembership ASP.NET.Identity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从MVC4到MVC5迁移并要使用ASP.NET身份太多,但我在努力寻找什么,涵盖一切,我想我需要做迁移的身份。 此链接表明我需要要做的就是创建一个 ApplicationUser 和迁移数据,和其他网络搜索给我的SQL脚本和passowrd散列建议。但也有,我想搞清楚之前,我在跳等枝节问题。

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. This link 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.

首先 - 我有code初始化会员制度在我的种子的方法:

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);

所以,需要向右走?

So that needs to go right?

二 - 它看起来像我需要一个 IdentityDbContext 。所以,我要改变我现有的上下文来继承?

Second - it looks like I need an IdentityDbContext. So should I change my existing context to inherit that?

即。而不是我目前的code的公共类SID2013Context:的DbContext 做到这一点:公共类SID2013Context:IdentityDbContext< ApplicationUser>

i.e. instead of my current code public class SID2013Context : DbContext do this: public class SID2013Context : IdentityDbContext<ApplicationUser>

将在该生成一个用于创建新表,我需要支持ASP.NET身份迁移?

Will that generate a migration that creates the new tables I need to support ASP.NET Identity?

一旦我已经做了我应该能够在的AccountController ,视图的ViewModels拉和启动是一个MVC5项目产生code。

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?

推荐答案

我意识到我链接到我的问题的网站告诉你如何迁移到身份表,但仍使用 SimpleMembership 。我想用整个身份,所以我使用Visual Studio这样我就可以在code,我想复制和修复它创建一个空的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:


  1. 新增 ApplicationUser.cs 我的模型项目

  2. 新增 IdentityConfig.cs 我的文件夹App_Start

  3. 新增 Startup.Auth.cs 我的文件夹App_Start

  4. 新增 AccountController.cs 我的Controllers文件夹(已更名为现有控制器)

  5. 新增 Startup.cs 来根文件夹

  6. 新增 AccountViewModels.cs 来的ViewModels文件夹

  7. 改变我的上下文继承 IdentityDbContext (它确实意味着新表在迁移创建)

  8. 添加了必需的NuGet包和​​固定了所有的命名空间

  9. 项目现在建立

  10. 添加迁移给了一个错误......的EntityType IdentityUserRole没有定义键

  11. ....解决了在我的覆盖将调用 base.OnModelCreating

  12. 添加迁移&安培; 更新 - 数据库 - 添加表所需

  13. 添加我的自定义字段中的用户配置 ApplicationUser 和更新数据库

  14. 使用了SQL(在迁移)复制旧表的新表中的数据。 SQL包括在这里。 NB需要登录过程中使用GUID来填充 SecurityStamp 来prevent误差

  15. 这是得到了登录和注销工作

  16. 删除用户配置 SimpleRoleProvider RoleManager 类 - 更换code其中必要的。

  17. 要删除引用 WebMatrix.Data WebMatrix.WebData 的dll

  18. 删除&LT;启用roleManager =真正的defaultProvider =简单&GT; &LT;会员defaultProvider =简单&GT; 从web.config中

  1. Added ApplicationUser.cs to my Models project
  2. Added IdentityConfig.cs to my App_Start folder
  3. Added Startup.Auth.cs to my App_Start folder
  4. Added AccountController.cs to my Controllers folder (renamed existing controller)
  5. Added Startup.cs to root folder
  6. Added AccountViewModels.cs to ViewModels folder
  7. Changed my context to inherit IdentityDbContext (it does indeed mean that new tables are created in a migration)
  8. Added the required NuGet packages and fixed up all the namespaces
  9. Project now builds
  10. But Add-Migration gave an error ... "EntityType IdentityUserRole has no key defined"
  11. ....solved that by adding a call to base.OnModelCreating in my override
  12. Add-Migration & Update-Database - adds the tables required
  13. Added my custom fields in UserProfile to ApplicationUser and updated the database
  14. 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
  15. That's got Login and Logout working
  16. Removed UserProfile, SimpleRoleProvider and RoleManager classes - replacing the code where necessary.
  17. Removed references to WebMatrix.Data and WebMatrix.WebData dlls
  18. Removed <roleManager enabled="true" defaultProvider="simple"> and <membership defaultProvider="simple"> from web.config

SQL在使用的第14步:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆