How to Apply ASP.NET Core Identity in ASP.NET Core

Hello Developers, Interesting topic on .NET CORE Identity and also we will discuss a few things about OWIN.

So Let’s Start.

Before starting Core Identity let’s be aware of OWIN.OWIN stands for Open Web Interface. NET.

Now Let’s talk about What is it and the main question Why do we use it?

OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open-source ecosystem of .NET web development tools.

In technical terms, we use OWIN as a middle layer. For example when you log in to a system that time OWIN is used to check the identity of that user.

Please check following the example of registration using OWIN.


public async Task Register(RegisterViewModel model)
{
	if (ModelState.IsValid)
	{
		var user = new ApplicationUser { UserName = model.UserName };
		var result = await UserManager.CreateAsync(user, model.Password);

		if (result.Succeeded)
		{
			await SignInAsync(user, isPersistent: false);
			return RedirectToAction("Index", "Home");
		}
		else
		{
			AddErrors(result);
		}
	}

	// If we got this far, something failed, redisplay form
	return View(model);
}

In this example, you can see that the create async method is used to create a user.

Now Let’s talk about Core Identity.

ASP.NET Core Identity has some default behaviors that you can override easily in your application’s Startup class.

Required Nuget Package: Microsoft.AspNetCore.Identity.EntityFrameworkCore.

Also Read: How To Add Watermark Text To Images in ASP.NET Using C#

In OWIN They Scaffold functionalities but in Core Identity we need to define it manually in startup class.

For example:


services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
	// Password settings
	options.Password.RequireDigit = true;
	options.Password.RequiredLength = 8;
	options.Password.RequireNonAlphanumeric = true;
	options.Password.RequireUppercase = true;
	options.Password.RequireLowercase = true;
	options.Password.RequiredUniqueChars = 2;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

In this example, you can see that there is a password validation.

When You set this in the configuration method of StartUp Class it will check when you sign in or register.

There is an also following functionality:

  • Two Factor Authentications for Authenticate using Pass Code
  • Configure Cookie Settings
  • Username Verification
  • Lockout Functionalities

These functionalities are based on the current version of ASP.NET Core 2.0.

FAQ 1: What are the benefits of ASP.NET Core Identity?

Yes, there are some benefits to it please Check the Following.

  • ASP.NET Core Identity supports all types of ASP.NET Core applications like Web Forms, MVC, Web API, SignalR or web pages. No need to learn different methods for MVC and Web Forms. Now one membership system supports all kinds of ASP.NET applications.
  • The new ASP.NET Core Identity system is easy to customize. You can add new fields to the user profile in no time.
  • ASP.NET Core Identity is designed based on interfaces which means highly customizable. If you don’t like one part of the system, you can replace it with your object.
  • The default implementation uses the Entity Framework code first. Since database schema is in your hands, you can easily change table names, primary keys, data types, etc.
  • By default, the system uses a SQL Server database. You can change that and use Oracle, MySQL, SharePoint, NoSql, Windows Azure Active Directory, and practically any other database.
  • ASP.NET Core Identity is highly testable. When MVC was introduced, one of the intentions was to enable unit testing in a web application. But, SimpleMembership still could not be tested. ASP.NET Core Identity resolves this problem too, and now all parts of the ASP.NET Core web application, including the membership system, are unit-testable.

FAQ 2: Do you have any Good References?

  • Configure ASP.NET Core Identity(https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?tabs=aspnetcore2x&view=aspnetcore-2.2)
  • ASP.NET Core MVC – Authentication And Role-Based Authorization With ASP.NET Core Identity(https://www.c-sharpcorner.com/article/asp-net-core-mvc-authentication-and-role-based-authorization-with-asp-net-core/)

I hope this article will be helpful to you.

Thank you…!!

Article by

Chintan Prajapati

Chintan Prajapati, a seasoned computer engineer with over 20 years in the software industry, is the Founder and CEO of Satva Solutions. His expertise lies in Accounting & ERP Integrations, RPA, and developing technology solutions around leading ERP and accounting software, focusing on using Responsible AI and ML in fintech solutions. Chintan holds a BE in Computer Engineering and is a Microsoft Certified Professional, Microsoft Certified Technology Specialist, Certified Azure Solution Developer, Certified Intuit Developer, and Xero Developer.Throughout his career, Chintan has significantly impacted the accounting industry by consulting and delivering integrations and automation solutions that have saved thousands of man-hours. He aims to provide readers with insightful, practical advice on leveraging technology for business efficiency.Outside of his professional work, Chintan enjoys trekking and bird-watching. Guided by the philosophy, "Deliver the highest value to clients". Chintan continues to drive innovation and excellence in digital transformation strategies from his base in Ahmedabad, India.