Configuring the Identity service

Enough with the theory, let's put the plan into action. Open the Startup.cs file and add the following highlighted lines:

[...]

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

// Add EntityFramework support for SqlServer.
services.AddEntityFrameworkSqlServer();

// Add ApplicationDbContext.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
);

// Add ASP.NET Identity support
services.AddIdentity<ApplicationUser, IdentityRole>(
opts =>
{
opts.Password.RequireDigit = true;
opts.Password.RequireLowercase = true;
opts.Password.RequireUppercase = true;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequiredLength = 7;
})
.AddEntityFrameworkStores<ApplicationDbContext>();

}

[...]

It's just like we did in Chapter 4, Data Model with Entity Framework Core, when we added the support for Entity Framework and registered the ApplicationDbContext in the Dependency Injection (DI) container; while we were there, we also took the chance to override some of the default password policy settings to demonstrate how we can configure the Identity service to suit our needs.

Note that the use of the IdentityRole class will require a reference to the Microsoft.AspNetCore.Identity namespace near the top of the file:

[...]

using Microsoft.Extensions.DependencyInjection;
using TestMakerFreeWebApp.Data;
using Microsoft.AspNetCore.Identity;

[...]
..................Content has been hidden....................

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