Introduction
This article explains how to get started with Identity.UI in ASP.Net Core MVC user authentication and registration.
Link to download the project source code here.
Step 1. Let's create an ASP.NET Core web application. Open Visual Studio 2019 and click on Create a new project.

Select ASP.NET Core Web Application, and click on Next.

Provide the project name of your choice, select the preferred location & click on Create.

Select MVC Template and click on Create, as shown below.

Step 2. Now let's add an ASP.NET Core Identity. Select the project > Right-click > Add > Click on the New Scaffold item.

Select Identity and click on add.

Now select the layout page, as we want authentication. Let's select login and Register as shown below provide the DbContext class and user class and click on Add.

We can find the Areas in the application with Data & Razor pages, as shown below.

Step 3. Now let's add user authentication to the application. Open the Startup class and Modify as shown below.
In order to add support to the razor pages, we have to call the function services.AddRazorPages() and endpoints.MapRazorPages().
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
Step 4. Now let's start with creating the database for the application.
Open UsingIdentityUser class and add the properties and decorate with the attribute PersonalData.
public class UsingIdentityUser : IdentityUser
{
[PersonalData]
[Column(TypeName = "nvarchar(100)")]
public string Firstname { get; set; }
[PersonalData]
[Column(TypeName = "nvarchar(100)")]
public string LastName { get; set; }
}
We have Dbcontext, which is also inherited from the parent class IdentityDB context. This identity db context is injected inside this identityhostingstartup class.
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
{
services.AddDbContext<UsingIdentityContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("UsingIdentityContextConnection")));
services.AddDefaultIdentity<UsingIdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<UsingIdentityContext>();
});
}
}
Now open appsttings.json. We can find the connection string with the name UsingIdentityContextConnection. By default, it will connect to the local DB.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"UsingIdentityContextConnection": "Server=SAGAR;Database=UsingIdentityDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Now open the Package manager console and Execute the command Add-Migration "First-Create" to generate the actual physical DB

Finally, execute the command Update-Database and you can find the new database as shown below.


Step 5. Now let's start customizing the application. Let's add a Nested layout for tab control headers.







HappyPosted Oct 13, 2022, 1:19 AM
Without downloading the project, the instructions are incomplete.
FPL PramitPosted Mar 22, 2021, 2:15 PM
After LogIn, the Hello @Username and logout doesn't appear