33

Part -1 | Asp.Net Core Identity Series[.NET 7] Introduction & Project Setup

 1 year ago
source link: https://www.learmoreseekmore.com/2023/02/part-1-dotnet7-aspnetcore-identity-series-intro-and-project-setup.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
NET%207%20Web%20API%20CRUD%20Using%20Entity%20Framework%20Core.png

The main objectives of the article are:

  • Introduction On Asp.NET Core Identity
  • Create Razor Page Application(.NET 7) With Asp.Net Core Identity
  • Generate Asp.Net Core Identity Tables
  • Default Registration & Login

Asp.Net Core Identity:

  • Asp.Net Core Identity is a built-in login functionality library that supports authentication of all different .NET applications like "MVC", "Razor Pages", "And Blazor Server".
  • Provides rich authentication UI pages which are customizable as well.
  • Adoptable for external authentication providers like 'Google', 'Facebook', 'Outlook', etc.
  • Can be integrated with other authentication like 'IdentityServer4', 'Azure Active Directory', 'Azure Active Directory B2C(Azure AD B2C)'.

Create A .NET7 Razor Page Application With Individual Authentication:

Let's create .NET7 Razor Page application with individual authentication to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any .NET7 application. For this demo I'm going to use the Visual Studio Code(using .NET CLI Commands).
Visual Studio 2022:
In Visual Studio 2022, select the 'Asp.Net Core Web App' template and choose the 'Authentication Type ' to 'Individual User Account'.
Visual Studio Code:
dotnet new webapp --auth Individual -uld -o Name_Of_Your_RazorPage_App

Create Identity Tables:

Using entity framework migrations we can run the default migration files in 'Data/Migrations' folder.
Visual Studio 2022:
In Package Manager Console run the following command
Update-Database
Visual Studio Code:
(Step 1) Make sure to install "dotnet ef" tools globally by running the below command
dotnet tool install --global dotnet-ef
(Step 2) Run the following command for migrations
dotnet ef database update
(Step 3) If migration fails(Setp 2 command), then delete the default migration files in the 'Data/Migration' folder, then create new migration files with the below command and then again run the step2 command
dotnet ef migrations add name_of_your_migration
Finally, our Identity tables get created.
1.PNG
  • The 'AspNetUsers' table stores all registered user information for authentication.
  • The 'AspNetRoles' table store the application roles.
  • The 'AspNetClimas' table for user-related claims.
  • The 'AspNetUserTokens' table for user authentication tokens
  • The 'AspNetUserLogins' table for maintaining authenticated providers(google, Facebook, etc) information.
  • The 'AspNetUserRoles' table for users mapped with roles.
  • The 'AspNetRoleClaims' table for roles with specified claims.

Generate Identity Files For Explicit Customization:

Default Asp.Net Core Identity files are bundled in the identity library. So to change the UI of 'Registration', 'Login', 'Forgot Password', etc pages we have to generate those files explicitly to override the existing ones.
Visual Studio 2022:
(Step 1) Install the below package
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
(Step 2)Right-click on the project -> Add -> New Scafolded Item. There you can choose options for scaffolding like selecting 'Layouts to override or not' , 'Existing Database Context or New Database context', etc.
Visual Studio Code:
(Step 1) First, let's install the 'dotnet-aspnet-codegenerator' globally on our system.
dotnet tool install -g dotnet-aspnet-codegenerator
(Step 2) Install the below package
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
(Step 3) Run the below command to scaffold all identity-related files
dotnet aspnet-codegenerator identity -dc {Your_Application_NameSpace}.ApplicationDbContext
On completion of scaffolding, we can observe all aspnet core identity files as below in the 'Areas' folder.
2.PNG

Default Registration And Login:

(Step 1) Now run the application => Click on Register Menu => Register with the application.
3.PNG
(Step 2) Since the default code doesn't have email-sending logic, it will display the email confirmation link on the page itself.
4.PNG
(Step 3) Confirm registration email
5.PNG
(Step 4) Click on the 'Login' menu => Enter credentials into the login form
6.PNG
(Step 5) After successful authentication.
7.PNG

In the next article, we are going to implement real email confirmation for user registration.

Wrapping Up:

Hopefully, I think this article delivered some useful information on the Asp.Net Core Identity In Razor Pages. using I love to have your feedback, suggestions, and better techniques in the comment section below

Follow Me:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK