Categories

Meta

Tag: .Net Core

Migration to ASP.NET Core Identity 2.0 is an important part of migration to ASP.NET Core 2.0 is. In our case of migration, we had to migrate from “AspNet Full Framework” to “AspNet Core 2.0”. All entities (models) in our case have to inherit from a specific base class and implement a specific interface. So adapting Identity to this, needs special considerations.

IdentityUser generic signature

First shocking change I saw, was IdentityUser. Former one allowed to introduce all other identity entities via its generic signature. Like this:

C#

1

IdentityUser<TKey,TLogin,TRole,TClaim>

But Identity Core has changed it to:

C#

1

IdentityUser<TKey>

It was easy to handle but in first glance it shocked me! This signature is not needed any more. Introducing it in just IdentityDbContext is enough:

New entities and field type changes

Identity Core 2.0 has introduced 2 new entities: RoleClaim and UserToken. Also date time columns in database tables have been changed to datetime2(7). Other changes also happened. For example, Role has 2 new fields, ConcurrencyStamp and NormalizedName.

JWT

Generating JWT seems to be easier than .Net Framework and OWIN. Formerly we have been using:

C#

1

2

3

Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerOptions

Microsoft.Owin.Security.ISecureDataFormat

Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerProvider

While a sample code for generating JWT in ASP.NET Core 2.0 is like this code (inspired from here):

In our way toward migrating an ASP.NET full framework application to ASP.NET Core 2.0 we encountered a significant change in EF Core 2.0 in comparison with EF 6.x.

EF 6.x

In EF 6.x we were tightly relying on automatic migrations in deployment operations. We never created or added a migration to the code-base. Instead we had turned on automatic migration and allowed data loss. So when new versions of the project were deployed on the server, all necessary changes on the database were done behind the scenes automatically.

Migrate() does not add or create a migration. It only checks if any not-applied migrations exists or not. If yes, then updates the database based on them.

EnsureCreated() creates the database based on the models in the project. But it does not do this in the migration way. Actually no migrations are needed by this method. Disadvantage of this method is that a database created by it, can not be updated in future by any migrations. Indeed this method is added to EF to help people create projects fast in MVP style.

Conclusion

At the end, we decided to not having automatic migrations as EF 6.x. Everyone that creates a model is responsible to create migrations too. And never call EF command in the production server to update the database. Instead call the Migrate() method on each startup to take the database to the latest available migration.

It is early September 2017. ASP.NET Core 2.0 has been out for a week or so. My computer has it. There is a project that has been started in early days of ASP.NET Core 1.0 beta days. That start day we did not used ASP.NET Core because of its beta nature. Instead we went with ASP.NET and OWIN.

Today that ASP.NET Core is mature enough, we are going to upgrade our project to ASP.NET Core 2.0 on .Net Core. Hopefully we can soon develop and run the project in Ubuntu in addition to Windows!

ASP.NET Core 2.0

Personally I have been working with Visual Studio Code for last year so I am comfortable with it. But as other teammates are using Visual Studio 2017 so I start my work from Visual Studio 2017. In future I can use Visual Studio Code in non fundamental code sessions.

Target project is a solution consisted of some projects. Each project is responsible for a set of functionalities. Project Domain for basic entities, enums, dtos, etc. Core mainly for business logic and Web for technologies related to web. Domain project is in lowest level, so I started with it.

Name-space and Nuget changes aside I encountered my first challenge while porting a class that was inheriting IdentityUser. Old identity can accept a row of generic parameters while Identity Core has changed them significantly.