Declaring the identity database context

Once we have declared the IUserRepository implementation, we can proceed by declaring the identity data context. The identity data context is identified by extending the IdentityDbContext class. This type of DbContext is used by EF Core to locate and access the data source used as the persistent user store. In order to declare the identity data context, it is necessary to extend the CatalogContext in the following way:

using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Catalog.Domain.Entities;
using Catalog.Domain.Repositories;
using Catalog.SchemaDefinitions;

namespace Catalog.Infrastructure
{
public class CatalogContext : IdentityDbContext<User>, IUnitOfWork
{
...

protected override void OnModelCreating(ModelBuilder
modelBuilder)
{
modelBuilder.ApplyConfiguration(new
ItemEntitySchemaDefinition());
modelBuilder.ApplyConfiguration(new
GenreEntitySchemaConfiguration());
modelBuilder.ApplyConfiguration(new
ArtistEntitySchemaConfiguration());

base.OnModelCreating(modelBuilder);
}
}
}
It is essential to note that the IdentityDbContext class extends the DbContext class. Furthermore, every property and behavior present in the DbContext class is also inherited by the IdentityDbContext class. Therefore, it is essential to note that the override method, OnModelCreating, must also call the base method.

To provide a way to store user information using EF Core, it is also necessary to add and configure the identity system for the specified User type by calling the AddIdentity extension method. Furthermore, it is also essential to call AddEntityFrameworkStores and refer to the CatalogContext class to add the entity framework implementation. The following code is the previously created AddAuthentication extension method:

using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Catalog.Domain;
using Catalog.Domain.Entities;

namespace Catalog.Infrastructure.Extensions
{
public static class AuthenticationExtensions
{
public static IServiceCollection AddTokenAuthentication(this
IServiceCollection services, AuthenticationSettings settings)
{
var key = Encoding.ASCII.GetBytes(settings.Secret);

services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<CatalogContext>();

...
return services;
}
}
}

Finally, we can proceed by initializing IUserRepository with its concrete implementation. Also, in that case, we will declare the dependency injection resolution in the Startup class:

        public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<IUserRepository, UserRepository>();

In the same way, we can register IUserService by adding the following row to the AddServices extension method:

public static IServiceCollection AddServices(this IServiceCollection services)
{
services
...
.AddScoped<IUserService, UserService>();

return services;
}

To sum up, now we have the whole authentication stack in place. The Catalog.API project exposes the HTTP routes through the UserController class. The controller depends on the IUserService interface, which exposes the operation needed by the authentication process.

Consequently, the UserService class depends on the IUserRepository interface, which is the main entry point that calls the API exposed by the EF Core framework. Therefore, we can now proceed by verifying the authentication logic using some tests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.2.122