Skip to main content

Posts

.NET5 Razor Pages CRUD Operations Using Entity Frame Work Core

In this article, we will implement a sample application .Net5 Razor Pages CRUD operations using entity framework core. Razor Pages: Razor Page is a simplified web application model. On comparing with 'MVC' template, razor pages won't have 'Controller', which means razor pages is a combination of 'Views' and 'Models'. Routing itself configured within the page or view. A razor page mostly contains 2 files like 'filename.cshtml'(view) and 'filename.cshtml.cs'(model). Create A .Net5 Razor Page Application: Begin our journey by creating a .Net5 razor page template application. Visual Studio users it is very easy to create razor applications by selecting the template option like 'ASP.NET Core Web APP'. Here I'm using a visual studio code editor and .Net CLI commands to generate the application. CLI Command To Create Razor Page Application: dotnet new webapp -n your_project_name After creating the project few basic things we

Clean Architecture In .Net5 Application

In this article, we will learn about Clean Architecture and then we will implement a .Net5 sample application. Clean Architecture: Clean Architecture core building blocks are: Application Core Infrastructure UI Application Clean Architecture lives on the dependency inversion principle. In general business, logic depends on the data access layer or infrastructure layer. But in clean architecture, it is inverted, which means data access layers or infrastructure layers depend on the business logic layer(which means Application Core). So with the dependency inversion technique it easy to configure 'Unit Test' or 'Integrating Test'. Application Core: Application Core is a top layer or parent layer which will not depend on any other layer. So other layers like Infrastructure or UI depend on the 'Application' core. Application Core contains 'Entites', 'DTOs', 'Interfaces', 'BusinessLogics', etc. So while creating projects if we want

NestJS JWT Auth Cookie Series - Part-3 - Refresh Token

In the  previous article , we understand the steps to generate the JWT token and store it in the cookie. Now here we will understand steps to protect API and also about refresh token. Install passport-jwt NPM Package: We have to create a new jwt passport strategy to validate the jwt token, so we need to install the below packages. Command To Install passport-jwt Packages: npm install --save passport-jwt npm install --save-dev @types/passport-jwt Install And Setup Cookie Parser: To read the cookie in the nestjs application we have to install the below plugin. Command To Install Cookie Parser: $ npm i cookie-parser $ npm i -D @types/cookie-parser Now configure the cookie parser 'main.ts' src/main.ts: Create JWT Passport Strategy: So to apply authentication to API's we have to validate our jwt token, so to do that we need to create a new jwt passport strategy. src/users/jwt.strategy.ts: import { Injectable, UnauthorizedException } from "@nestjs/common"; im

NestJS JWT Auth Cookie Series - Part-2 - Generating Access Token

In this article, we target to generate the jwt authentication and store it in the HttpOnly cookie for user authentication.  Part-1  completely explains implementing user registration in the nestjs application. Implement Logic To Validate User Credentials: First, let's create a model to store the valid user. src/models/current.user.ts: export class CurrentUser { userId: number; firstName: string; lastName: string; email: string; } Now we have to implement the logic for the login endpoint that is to validate user email and password. src/users/users.service.ts: public async validateUserCredentials(email: string, password: string):Promise<CurrentUser> { let user = await this.user.findOne({ email: email }); if (user == null) { return null; } const isValidPassword = await bcrypt.compare(password, user.password); if (!isValidPassword) { return null; } let currentUser = new CurrentUser(); currentUser.userId = user.userId;

NestJS JWT Auth Cookie Series - Part-1 - User Registration

This is the first installment of the NetsJS JWT Auth Cookie Series. In this part our main focus on user registration by the NestJS endpoint. PostgreSQL Database: For this demo, I'm using the free open-source PostgreSQL database. Here I'm going to use the PostgreSQL docker image because it is easy and fast to set up and configure.  Click here to getting started with PostgreSQL docker . Run the following database query to create the 'User' table. CREATE TABLE User( UserId SERIAL PRIMARY KEY NOT NULL, FirstName VARCHAR(200) NULL, LastName VARCHAR(200) NULL, Email VARCHAR(200) NOT NULL, Password VARCHAR(200) NOT NULL, RefreshToken VARCHAR(1000) NULL, RefreshTokenExp DATE NULL ) Create A NestJS App: Let's begin our demo by creating a sample NestJS application. Command To Install NestJS CLI npm i -g @nestjs/cli Command To Create App nest new your_project_name Install ORM And PostgreSQL NPM Packages: ORM packages are essential to install because they prov