How to Apply Asp.Net Core Identity in Asp.Net Core

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

So Let’s Start.

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

Now Let’s talk about What is it and main question Why 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 term, we use OWIN as a middle layer. For example when you log in to a system that time OWIN used to check the identity of that user.

Please check following the example of registration using OWIN.

public async Task<ActionResult> 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 create async method use 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.

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<ApplicationDbContext>()
    .AddDefaultTokenProviders();

RELATED: What is ASP.NET Core? Advantage(Pros) of ASP.NET Core Technology

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

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

So AspNet Core Identity also work as a middle layer of Controller and Database. So when you Authenticate first it will check with Core Identity then get data.

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 current Version Asp Net Core 2.0.

Q: Any Benefits of Asp Net Core Identity?

  • Yes, There are some benefits of it please Check 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.
  • 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 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 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 is 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 ASP.NET Core web application, including membership system, are unit testable.

Any Good References?

By | 2018-01-13T18:25:03+00:00 January 4th, 2018|Asp.Net, ASP.NET Core, Asp.Net MVC|