Add Authentication Using ASP.NET Core Identity

1. Install Required Packages

Open Package Manager Console and run:

Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore
Install-Package Microsoft.AspNetCore.Identity.UI

These packages allow you to use built-in authentication UI and database tables.

2. Update Your DbContext to Support Identity

Open ApplicationDbContext.cs.

Replace:

public class ApplicationDbContext : DbContext

With:

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

public class ApplicationDbContext : IdentityDbContext

Now EF Core will automatically include Identity tables.

3. Register Identity in Program.cs

Open Program.cs and update the configuration:

builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});

// Add Identity
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

// Add Identity UI
builder.Services.AddRazorPages();

Also add:

app.MapRazorPages();

4. Add Identity Tables to Database

Run the following commands:

Add-Migration AddIdentityTables
Update-Database

This will automatically create approximately 15 tables, including:

  • AspNetUsers

  • AspNetRoles

  • AspNetUserRoles

  • AspNetUserClaims

  • AspNetUserLogins

  • And more related Identity tables

5. Add Identity UI Pages (Login, Register, Logout)

Add the following in _Layout.cshtml to include login/logout UI:

<div class="navbar-nav">
    <partial name="_LoginPartial" />
</div>

Identity automatically provides a _LoginPartial.cshtml.

If it is missing, you need to scaffold Identity.

6. Scaffold Identity (Optional, but Recommended)

Right-click your projectAddNew Scaffolded Item.

Choose:

Identity → Add

Select the pages you want, such as:

  • Login

  • Register

  • Logout

  • Manage

Select your ApplicationDbContext.

This generates files such as:

Areas/Identity/Pages/Account/Login.cshtml
Areas/Identity/Pages/Account/Register.cshtml
Areas/Identity/Pages/Account/Logout.cshtml

These pages now work out of the box.

7. Add Authorization to Your Controllers

To protect any controller or action, use the Authorize attribute.

using Microsoft.AspNetCore.Authorization;

[Authorize]
public class ProductController : Controller
{
}

If a user is not logged in, they will automatically be redirected to the login page.

8. Allow Anonymous Access Where Needed

Some pages may need to remain publicly accessible, such as the home page or product list.

Example:

[AllowAnonymous]
public IActionResult Index()
{
    return View();
}

9. Example: Register New User

Run your application and click Register.

Enter:

  • Email

  • Password

  • Confirm Password

After registration, a new row will be inserted into the AspNetUsers table.

10. Example: Login

Use the same credentials to log in.

ASP.NET Core Identity automatically:

  • Validates the password

  • Sets the authentication cookie

  • Manages the user session

11. Logout

Generate a logout link using the following form:

<form asp-area="Identity" asp-page="/Account/Logout" method="post">
    <button type="submit" class="btn btn-link">Logout</button>
</form>

12. Protect Product Module

Open ProductController.cs and apply the Authorize attribute.

[Authorize]
public class ProductController : Controller
{
}

Now only logged-in users can Add, Edit, or Delete products.

13. Summary of Part 4

You added the following features to your application:

  • Identity packages installed ✔

  • Identity database tables created ✔

  • Register and Login pages available ✔

  • Authentication cookies enabled ✔

  • Authorization using [Authorize]

  • Protected controller access ✔

  • Identity UI integrated ✔

Your project is now running full authentication similar to a professional ASP.NET MVC application.