Posts

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...

GraphQL Hello World in .NET

Image
GraphQL is the query language for the API. With an API endpoint, any data can be retrieved from the server. You can tell the API what data you want, the API will return those values. So it helps to avoid multiple requests and instead of getting all the data and selecting the one you want, you can tell the API what you want and it will send that data. This article explains how to create the first basic GraphQL in .NET. Let’s look at it with some sample code. First, you have to install the GraphQL package. You can install the latest version of GraphQL from NuGet. The following is a command to install the GraphQL package PM> Install-Package GraphQL Next, you need to install the JSON.NET serializer for GraphQL.NET. The below command installs NewtonsoftJson for the application. PM> Install-Package GraphQL.NewtonsoftJson The following is the .NET C # console application. First I added the necessary packages. Then the schema was initialized. The schema states what the requ...

Debugging Blazor WebAssembly Application in Browser

Image
This article is going to explain how to debug the Blazor WebAssembly application on the Chrome browser's Dev tool. Let’s look at it step by step Step 1: I have developed a small calculate application in Blazer to demonstrate the debugging process. It calculates simple arithmetic. If users click the Calculate button it will display the results. The following is a screenshot of the calculated code snippet. Step 2: Press Shift+Alt+D (When focusing the application). This will open a new tab with the message “Unable to find debuggable browser tab” . But at the same time, the page gives instructions on how to get this. Step 3: Press Win + R . This will open a run window. Enter the command specified on the page in the open text box (chrome --remote-debugging-port=9222 --user-data-dir="C:\Users\golda\AppData\Local\Temp\blazor-chrome-debug" https://localhost:44366/calculate). And click ok. This will reopen the application in another Chrome bro...

Progressive Web App (PWA) Manifest File in Blazor

Image
This article is going to explain what a manifest file is and how to use it in the PWA Blazor application. You can easily configure the manifest in your Blazor app. Let's see how to do it. What is a Manifest File? Manifest is a single JSON file that tells the browser what kind of application it is, What is the name of the application? Where is the application icon? What background color and etc. The manifest file name must be manifest.json While creating the Blazor application , it will automatically create the manifest.json file under wwwroot folder. The following is a default manifest.json code. { "name" : "SamplePWABlazorApp" , "short_name" : "SamplePWABlazorApp" , "start_url" : "./" , "display" : "standalone" , "background_color" : "#ffffff" , "theme_color" : "#03173d" , "icons" : [ { "src" : "icon-5...