Posts

Showing posts from December, 2020

Simple Cookie Authentication in ASP.NET Core

Image
Authentication is the process of verifying that the user has access to the application. There are several ways to achieve this in ASP.NET Core. This blog is going to explain how to implement it on ASP.NET Core MVC. Gradually, we will see how to implement it Step 1: First, you need to add AddAuthentication to ConfigureServices. That registers the services required for authentication services. The following code registers the cookie authentication services. Startup.cs public void ConfigureServices( IServiceCollection services) { ... ... ... services.AddAuthentication( CookieAuthenticationDefaults .AuthenticationScheme) .AddCookie(options => { options.Cookie.Name = "SampleCookieAuth" ; options.LoginPath = "/Account/Login" ; }); services.AddControllersWithViews(); } Next, you have to add the UseAuthentication extension method above the UseAuthorization method. It adds authentication m

Custom Attribute Validation in ASP.NET Core

Image
This blog is going to explain what is the Custom Attribute Validation in ASP.NET Core. When the built-in verification attributes are not sufficient to handle validation conditions, you can create your own custom validation attributes. Let’s see how to achieve this step by step. Step 1: The following is the WeeklyEmailAttribute.cs class. Here I have implemented the ValidationAttribute class and override the IsValid method. The IsValid method verifies that the IsAgreed property is true and that the email address is empty. If the condition is true, it will issue a validation message. Otherwise, it will return a success verification result. WeeklyEmailAttribute.cs using System.ComponentModel.DataAnnotations; namespace SampleWebApplication.Validation { public class WeeklyEmailAttribute : ValidationAttribute { protected override ValidationResult IsValid( object value , ValidationContext validationContext) { var sample = (Samp

IValidatableObject: Class-Level Validation in ASP.NET Core

Image
This blog is going to explain what class-level validation is in ASP.NET Core. ASP.NET Core has some built-in validation attributes, such as [Required], [Range], [EmailAddress]. But those verification attributes are not enough all the time. Sometimes we need to validate based on conditions. In that case, you can inherit and validate the IValidatableObject for the class. This validation only works in the class where you implement the IValidatableObject . This example illustrates how to check the text box content based on the checkbox value. That means if the checkbox is checked it should validate whether the text box is empty or not. Let's look at that step by step. Step 1: The following is a SampleModel class. In this class, I implemented IValidatableObject . This class has only two properties, Join and PhoneNumber. Join property is a bool and PhoneNumber is a string. Also, the Validate method is implemented from IValidatableObject. In the method, I have checked whether t

[Remote] Attribute Validation in ASP.NET Core

Image
This blog is going to explain what Remote Attribute Verification is and how to use it. In this validation, the Controller action is called using the client-side script. This allows you to verify the server-side code without page refresh. This is because the server-side method is called by the jQuery validation method in the ASP.NET core. Let’s look at it step by step. Step 1: jQuery Library You have to make sure you have the following jQuery. As I mentioned earlier, the server-side controller action method is called by jQuery validation. < script src = "~/lib/jquery/dist/jquery.min.js" ></ script > < script src = "~/lib/jquery-validation/dist/jquery.validate.min.js" ></ script > < script src = "~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js" ></ script > Step 2: Model Next step I have created a Model called MovieModel. It has two properties, MovieName and ReleaseYear. To demons