Your apps may implement authentication quite easily with the help of Microsoft’s ASP.NET framework. Hardly little has to be done to create a standard web application. You need to do little more than tick a few boxes to proceed.

You may not be aware, though, that ASP.NET user authentication may be added to console applications.

Thankfully, the hidden magic is mostly gone with.NET Core. All the components needed for authentication are available to you, and you are free to reuse them as necessary.

Making two projects—a console application and a web application—is the simplest method to do this. Make sure ASP.NET authentication is turned on in the web application before you begin.

All that is left to do is transfer some settings from the web application to our console application.

We must first include a few NuGet packages in our console application. References will be required for:

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

You must first construct your NuGet packages before creating the ApplicationUser class and the Entity Framework database context.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    public DbSet<ApplicationUser> ApplicationUsers { get; set; } 
}
public class ApplicationUser: IdentityUser
{

}

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

public class Configurator
{
    public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite(
                configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddLogging();

        services.AddScoped(
            typeof(IAuthentication),
            typeof(Authentication));
    }
}

Now that everything has been set up, we can use our console application to access ASP.NET Identity data just as we would in a web application. A example authentication service with numerous typical user-related features is shown below.

public class Authentication : IAuthentication
    {
        readonly UserManager<ApplicationUser> _userManager;

        public Authentication(UserManager<ApplicationUser> userManager)
        {
            _userManager = userManager;
        }
        
        public async Task<User> CreateUser(string email, string password)
        {
            var applicationUser = new ApplicationUser()
            {
                Email = email,
                UserName = email,
            };

            
            var result = await _userManager.CreateAsync(applicationUser, password);
            if (!result.Succeeded)
            {
                return null;
            }
            
            var loaded = await _userManager.FindByNameAsync(email);
            return DTOMapper.Map<User>(loaded);
        }
        
        public async Task<User> UpdatePassword(string email, string oldPassword, string newPassword)
        {
            var loaded1 = await _userManager.FindByNameAsync(email);
            if (loaded1 == null)
            {
                return null;
            }
            
            var result = await _userManager.ChangePasswordAsync(loaded1, oldPassword, newPassword);
            if (!result.Succeeded)
            {
                return null;
            }
            
            var loaded2 = await _userManager.FindByNameAsync(email);
            return DTOMapper.Map<User>(loaded2);
        }
        
        public async Task<bool> CheckPassword(string email, string password)
        {
            var loaded1 = await _userManager.FindByNameAsync(email);
            if (loaded1 == null)
            {
                return false;
            }
            
            return await _userManager.CheckPasswordAsync(loaded1, password);
        }
    }

Leave a comment

Your email address will not be published.