ASP.NET Core - Identity Overview


Advertisements


In this chapter, we will discuss the ASP.NET Core Identity framework in brief. ASP.NET Core Identity framework is used to implement forms authentication. There are many options to choose from for identifying your users including Windows Authentication and all of the third-party identity providers like Google, Microsoft, Facebook, and GitHub etc.

  • The Identity framework is another dependency that we will add to our application in the project.js file.

  • This framework allows us to add features where users can register and log in with a local password.

  • The framework also supports two-factor authentication, third-party identity providers and other features.

  • We are going to focus on the scenarios where a user can register and log in and log out.

To do this, we need to create a User entity and this class will inherit from a base class in the Identity framework, and the base class gives us our standard user properties like username and email address.

Identity Overview
  • We can include as many additional properties as we want on this class to store information about our users.

  • We need to take this User class and plug it into a UserStore class provided by the Identity framework.

  • The UserStore is the class that our code will talk to create users and validate user passwords.

  • Ultimately, a UserStore will talk to a database. The Identity framework supports the Entity Framework and all of the databases that can work with the Entity Framework.

  • But you can implement your own UserStore to work with any data source.

  • In order to work with the Entity Framework properly, our User class will also plug into an IdentityDb class.

  • This is a class that uses an Entity Framework DBContext to do the actual database work.

  • We will need to include this IdentityDb into our application by having our existing DataContext class inherit from the IdentityDb instead of the Entity Framework's DBContext.

  • It is the IdentityDb and the UserStore that work together to store user information and validate user passwords, the hashed passwords that are in the database.

There are two pieces of the ASP.NET Core Identity Framework that we need to know

Core Identity Framework

SignInManager

This is one of the two pieces of the Identity framework −

  • As the name implies, the SignInManager can sign in a user once we validate the password.

  • We can also use this manager to sign a user out.

  • With forms authentication, the sign in and the sign out are done by managing a cookie.

  • When we tell the SignInManager to sign a user in, the manager issues a cookie to the user's browser, and the browser will send this cookie on every subsequent request. It helps us identify that user.

Identity Middleware

This is the second piece of the framework −

  • Reading the cookie sent by the SignInManager and identifying the user, this happens in the final piece of the framework, the Identity Middleware.

  • We will need to configure this middleware into our application pipeline to process the cookie set by the SignInManager. We will also see some other features of this middleware in the next few chapters.



Advertisements
E-Books Store