Identity is a secured way of authentication methods in web applications. ASP.NET Core Identity is the membership system for building ASP.NET Core web applications, including membership, login, and user data. ASP.NET Core Identity allows developers to add login features to application and makes it easy to customize data about the logged in user.
public class Seed
{
public static async Task CreateRoles(
IServiceProvider serviceProvider,
IConfiguration Configuration)
{
//adding customs roles
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole >>();
var UserManager = serviceProvider.GetRequiredService<UserManager<IdentityUser >>();
string[] roleNames = { "Administrator", "Customers" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
//creating the roles and seeding them to the database
var roleExist = await RoleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
}
}
}
}
By default, the ASP.NET Core Identity system stores user information in a SQL Server database using Entity Framework Core. For many apps, this approach works well. However, we may prefer to use a different persistence mechanism.
EF Core In-Memory Database Provider allows Entity Framework Core to be used with an in-memory database. This can be useful for testing, although the SQLite provider in in-memory mode may be a more appropriate test replacement for relational databases.
public class Startup
{
...
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<AppDbContext>
(options => options.UseInMemoryDatabase("DB"));
...
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdminRole", policy => policy.RequireRole("Administrator"));
});
...
}
}
var host = BuildWebHost(args);
using (var scope = host.Services.CreateScope()) {
var services = scope.ServiceProvider;
try {
var serviceProvider = services.GetRequiredService<IServiceProvider>();
var configuration = services.GetRequiredService<IConfiguration>();
Seed.CreateRoles(serviceProvider, configuration).Wait();
}
catch (Exception exception) {
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(exception, "An error occurred while creating roles");
}
}
host.Run();
A confirmation email is an email that is sent to a person in order to confirm about registration.
Diagram of email verification.
Top