Saturday, October 29, 2016

Migrating Asp .Net MVC to .Net Core .. Authentication

Hi all, long time no post...

I am learning the new .NET CORE project type now, and want to rewrite my application from the old ASP .NET MVC in the new project template. Reasons for this for me, are really just that my old project is AngularJS 1.0, and I want to try to rebuild using Angular 2.0 with Typescript.

So one stumbling block I hit was the Identity and Authentication database from the old project would not work with the new project. It gave some errors about certain columns missing. What I have had some partial success with is a migration script to update the old DB. This has allowed me to login using a user name and password, and to create a new user. That is the extent of my testing :


Alter Table ASPNETROLES
ADD
 ConcurrencyStamp varchar(255) null,              
 NormalizedName varchar(255) null

 Drop Table AspNetUserTokens

 CREATE TABLE [AspNetUserTokens] (
    [UserId]        NVARCHAR (450) NOT NULL,
    [LoginProvider] NVARCHAR (450) NOT NULL,
    [Name]          NVARCHAR (450) NOT NULL,
    [Value]         NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_AspNetUserTokens]
PRIMARY KEY CLUSTERED ([UserId] ASC, [LoginProvider] ASC, [Name] ASC)
)

Alter Table AspNetUsers
 Add
 ConcurrencyStamp varchar(255) null,
 LockoutEnd DateTime null,
 NormalizedEmail varchar(255) null,
 NormalizedUserName varchar(255) null

Drop Table [AspNetRoleClaims]

CREATE TABLE [AspNetRoleClaims] (
    [Id]         INT            IDENTITY (1, 1) NOT NULL,
    [ClaimType]  NVARCHAR (MAX) NULL,
    [ClaimValue] NVARCHAR (MAX) NULL,
    [RoleId]     NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_AspNetRoleClaims]
PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId]
FOREIGN KEY ([RoleId])
REFERENCES [dbo].[AspNetRoles] ([Id]) ON DELETE CASCADE
)


GO
CREATE NONCLUSTERED INDEX [IX_AspNetRoleClaims_RoleId]
    ON [AspNetRoleClaims]([RoleId] ASC)

Alter Table AspNetUserLogins
   Add  ProviderDisplayName varchar(255) null





You may find this discussion relevant if you have gotten this error :
SqlException: Invalid column name 'NormalizedUserName'. Invalid column name 'ConcurrencyStamp'. Invalid column name 'LockoutEnd'. Invalid column name 'NormalizedEmail'. Invalid column name 'NormalizedUserName'.

2 comments:

Brian O said...

There is a nicely asked question about this on StackOverflow, here :
http://stackoverflow.com/questions/38315812/use-a-identity-2-0-database-to-authenticate-a-asp-net-core-1-0-application

Brian O said...

A better link to the StackOverflow thread :
http://stackoverflow.com/q/38315812/971356