How to move from SimpleMembership to ASP.NET.Identity

Migrating from MVC4 to MVC5 and you want to use the ASP.NET identifier, but I'm struggling to find something that covers everything I think I need to do to port Identity. Migrating an existing website from SimpleMembership to Identity ASP.NET offers all I need to do is create ApplicationUserand migrate the data, as well as other web searches give me sql scripts and passowrd hashing tips. But there are other loose ends that I would like to clarify before jumping in.

First, I have code to initialize the membership system in the Seed method:

if (!WebSecurity.Initialized)
    WebSecurity.InitializeDatabaseConnection(Config.ConnectionStringName,
        Config.UsersTableName,
        Config.UsersIDColumn,
        Config.UserNameColumn,
        autoCreateTables: true);

So what do you need to go right?

The second - it seems I need IdentityDbContext. So should I change my existing context in order to inherit this?

i.e. instead of the current code, public class SID2013Context : DbContextdo the following:public class SID2013Context : IdentityDbContext<ApplicationUser>

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

As soon as I do this, I can use AccountController, Views, ViewModels and the startup code created for the MVC5 project.

Can someone answer specific questions here and / or point me to the best resource?

+3
source share
1 answer

, , , , Identity, SimpleMembership. Identity , MVC Visual Studio, , , . , :

  • ApplicationUser.cs
  • IdentityConfig.cs App_Start
  • Startup.Auth.cs App_Start
  • AccountController.cs ( )
  • ​​ Startup.cs
  • ​​ AccountViewModels.cs ViewModels
  • IdentityDbContext ( , )
  • NuGet
  • Add-Migration ... "EntityType IdentityUserRole , "
  • .... , base.OnModelCreating
  • Add-Migration Update-Database -
  • UserProfile ApplicationUser
  • sql ( ) . Sql. NB SecurityStamp
  • .
  • UserProfile, SimpleRoleProvider RoleManager - .
  • WebMatrix.Data WebMatrix.WebData dlls
  • <roleManager enabled="true" defaultProvider="simple"> <membership defaultProvider="simple"> web.config

Sql, 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)
+8

All Articles