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 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 and provide 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; }
-
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllersWithViews();
- services.AddRazorPages();
- }
-
-
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
-
- 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.
Select Razor layout and click on Add
Customize the layout as below
- @{
- Layout = "/Views/Shared/_Layout.cshtml";
- }
- <div class="row">
- <div class="col-md-6 offset-md-3">
- <div class="card login-logout-tab">
- <div class="card-header">
- <ul class="nav nav-tabs card-header-tabs">
- <li class="nav-item">
-
- <a class="nav-link" href="/Identity/Account/Login">Log In</a>
- </li>
- <li class="nav-item">
-
- <a class="nav-link" href="/Identity/Account/Register">Sign Up </a>
- </li>
- </ul>
- </div>
- <div class="card-content">
-
- <div class="row">
- <div class="col-md-12 ">
- @RenderBody()
- </div>
- </div>
- </div>
-
- </div>
- </div>
- </div>
- @section Scripts{
-
- @RenderSection("Scripts", required: false)
- <script>
- $(function () {
-
- var current = location.pathname;
- $('.nav-tabs li a').each(function () {
- var $this = $(this);
- if (current.indexOf($this.attr('href')) !== -1) {
- $this.addClass('active');
- }
- })
- })
- </script>
- }
Open site.css and use the below styles
-
- div.login-logout-tab div.card.header{
- padding: 0px 0px 12px 0px;
- }
- div.login-logout-tab li.nav-tabs{
-
- margin: 0px 0px -12px 0px;
- }
- div.login-logout-tab li.nav-item{
-
- width:50%
- }
- div.login-logout-tab a.nav-link{
- font-size:25px;
- color:#495057;
- text-align:center;
- }
Modify the Login page and register the HTML pages with our new layout.
In order to display the active tab, we need to add an active class, as shown below.
- @section Scripts{
-
- @RenderSection("Scripts", required: false)
- <script>
- $(function () {
-
- var current = location.pathname;
- $('.nav-tabs li a').each(function () {
- var $this = $(this);
- if (current.indexOf($this.attr('href')) !== -1) {
- $this.addClass('active');
- }
- })
- })
- </script>
- }
Let's see the output and register a user.
Now let's login with the registered user.
Conclusion
In this article, we discussed how to use Identity UI in ASP.NET Core MVC application by creating a database using the package manager console & commands. I hope you all enjoyed reading this and learned from it.