Migrating to Identity Core 2.0

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:

But Identity Core has changed it to:

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:

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

BTW it seems that JWT consumption has not been changed a lot. In .Net Framework/OWIN we had UseJwtBearerAuthentication and in ASP.NET Core 2 we have AddJwtBearer. They both accept some configurations.

See a sample generated JWT using ASP.NET Core 2.0 below.

JWT
JWT

Migrating to ASP.NET Core 2.0

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

Old Identity User
Old Identity User
Identity Core User
Identity Core User

This story is going to be continued…

Developing an ASP.NET Core project on Ubuntu

It's a long time that I'm trying to migrate from Windows/.Net to Linux. But job market have more demands on .Net other than none Microsoft technologies. My hope was on ASP.NET Core. It was not very good at the Beta/RC time but now that is published in final version, my mind has been changed. It now works good on Ubuntu.

 

At start I created several test projects with ASP.NET Core on both Windows and Ubuntu. I wanted to be sure that ASP.NET Core is capable enough to rely on it in a real world project. I tried to test all aspects of MVC, Web API, Razor, Nuget packages, EF Core, Identity Core, internal DI/IoC, project.json, SQLite, unit testing and tooling include Visual Studio Code, debugging, unit testing, auto complete, code formatting, shortcuts, etc. I tested them on a Windows machine and on an Ubuntu machine with both Unity and Xfce desktops. All tests showed that I will not encounter a big problem utilizing ASP.NET Core itself as first place and using it in Ubuntu at second place.

 

Using .Net Core in Ubuntu is as same as Windows except that you rely more on terminal than GUI. Using dotnet commands are exactly same in both Windows and Ubuntu. Same names, same switches, same operations and same outputs. It was really good thing that they are exactly same. But when it comes to tooling it is different. While your IDE in both platforms is Visual Studio Code they do not differ very much but if you are used to use Visual Studio 2015 then you can understand how deep is difference between Visual Studio Code and Visual Studio 2015. The latter have a full integration and do everything you need with just pressing some shortcuts. But Visual Studio Code needs many configurations to behave like Visual Studio 2015. Debugging in Visual Studio Code is not as easy as Visual Studio 2015, you need some level skill of operating system to be able to debug codes in Visual Studio Code. My good friend from Visual Studio 2015, IntelliSense, was not working at all at first days. Now that it works, it does not work as good as in Visual Studio 2015. It shows many un-related items too. BTW having Visual Studio Code in Ubuntu is like a miracle. It is very similar to Visual Studio 2015, it has code highlighting, similar shortcuts (Unity desktop), good integration with git, real time compile (to show errors in the code), etc. Did I mentioned that you can use yeoman as a substitution of Visual Studio 2015 templating system that is absent in Visual Studio Code?

 

The project I am working on it in Ubuntu is a regular web application with parts rendered via MVC and parts delivered to a mobile app as Web API. In development environment I use SQLite as database backend but for production we will be using MS-SQL Server. EF Core works good despite its constraints in verison 1. SQLite in other part also works good as development environment database. It does not allow complete support of EF migration but instead works same from Ubuntu and from Windows. One thing that works great is that the code I'm working on is working exactly same in Windows and Ubuntu. I change code in my Ubuntu machine, commit and push them to the server then pull it from a Windows machine and then continue my development from my Windows machine, no matter I have switched from one OS to another OS. Code behaves same in both OSes and runs exactly same. Additionally code can be developed and run in both Visual Studio Code and Visual Studio 2015 as same. The only consideration is directory structure be designed compatible with structure that Visual Studio 2015 knows about.

 

I haven't yet deployed the project into a Linux machine as our client probably prefer to use Windows for it. But I hope hosting would not be a problem too. As a long term .Net developer I am very exited about cross platform feature that Microsoft has been added to .Net but frankly I'm a bit worry about it. I'm not sure if Microsoft would continue this way on Linux or not. I am afraid that developers using .Net in Linux would not be as big as Microsoft imagines then abandon it.

 

I started developing ASP.NET Core on Ubuntu on Unity (standard desktop with Ubuntu). Everything was good except high CPU usage. This problem was not caused by ASP.NET Core instead it was caused by 'hud service' from Unity. For this reason I decided to try Xfce also. It is a light desktop that does not have hud service high CPU usage problem but have problems of its own kind. First thing you encounter is that shortcuts are very very different than Unity. I lost even my Ctrl+F3 (for searching keywords in Visual Studio Code). In rare situations it has problems with high CPU usage of Visual Studio Code (OmniSharp) but bigger problem is regular crashes of my applications like Toggl, my favorite time tracker, and even Google chrome. Though I'm still using Xfce but think I would soon switch back to Unity and find another solution for hud service high CPU usage.

 

Please see some pictures of my experiences:

asp-net-core-debug-ubuntu-xfce omni-sharp-high-cpu-usage xfce-chrome-crashes

 

Custom authentication in Asp.Net Core

ASP.NET Core does not have built-in membership for Active Directory. Developers can use Azure Active Directory or external membership services like Facebook and Linkedin or standard ASP.NET Identity mechanism, but are not able to use Active Directory directly. Visual Studio 2015 has removed Active Directory authentication from its web application template:

asp-net-core-authentication-active-directory

 

ASP.NET Core does not have web.config so old school ActiveDirectoryMembershipProvider does not help. ASP.NET Core use middleware mecahnism for handling requests. Settings cookies and checking it back in the server has been changed.

 

In order to do authentication in ASP.NET Core with Active Directory or any other resource like your own database back end you can take a look at following code.


 

This code must be put in Configure method of Startup class of the project. This tells application to use cookie for authentication. Every information is put in cookie, attached to response and sent back to the user. User attaches this cookie to all his/her requests and sent to the server again. This cookie is encrypted and contains Username, Role and other claims added by server code.

 


 

This code in used in sign in page. This is the place that you validate user entered credentials against a database or resource like Active Directory. For more information see here and here. Also I found a library for the whole process but was not successful to use it.