Using ASP.NET Identity in a Console App? Yes You Can!

Microsoft’s ASP.NET framework provides a really easy way to add authentication to your applications. For a classic web application, you almost don’t have to do anything. Just check a couple of check boxes and you are good to go.

But did you know it’s possible to add ASP.NET user authentication to a console application?

Luckily with .NET Core, the hidden magic is pretty much gone. You have access to all of the pieces required for authentication and can reuse those components as needed.

The easiest way to accomplish this is to create two projects: a console application and a web application. Before you start, make sure you have enabled ASP.NET authentication in the web application.

We just need to move some configuration from the web application to our console application.

First, we need to add a few NuGet packages to our console application. We will need to reference:

  • Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
  • Microsoft.AspNetCore.Identity.UI

After you have your NuGet packages created, you need to create an Entity Framework database context and an ApplicationUser class.

After getting your Entity Framework stuff set up, all that is really left to configure is your .NET Core DI.

Now that we have all of this configured, we can consume ASP.NET Identity data like we would in a web app, right from within our console application. Below is a sample Authentication service that includes many common user-related features.

Related posts