Posts

Showing posts with the label C#

Human Captcha (Not Robot) React Component with .Net Core WebApi Backend

Image
A captcha is a challenge response test to determine whether or not the user is human. We may create a React human captcha component with a .Net Core Web API back-end.  Back-end Our back-end is ASP.Net Core Web API 3.1. Back-end will generate a truly random alpha-numeric captcha value, creates a binary image using System.Drawing namespace. Back-end will verify the user input value against the generated value and  return an authentication Token to be used in Forms. The method that generates a random captcha value and creates a binary image is as follows:         /// <summary>         /// generate captcha image         /// </summary>         /// <param name="size">alphanumeric length of the captcha</param>         /// <returns>image properties and image binary data</returns>         private async Task<CaptchaImage> gener...

Custom ActionResult for Files in ASP.NET MVC - ExcelResult

An action result is the standard result from an action.  Action results handle the execution of the result requested by an action method.  Although standard action results FileContentResult and FileStreamResult may be used for downloading files that are generated on the fly, creating a custom action result might be the best solution. For situations that are not supported by  standard action results, we may create custom action results. For example w e may create custom  action result for creating PDF, Excel or Word files on the fly for download or generating and returning images.  Creating a new custom action result requires that our class inherit the abstract class  System.Web.Mvc. ActionResult and override the  ExecuteResult method. As an example let's create a custom action result for exporting data to Excel files on the fly for download. We are using FastMember package for creating DataTable from IEnumerable object and ClosedXML package ...

ASP.NET MVC Filter Based Authorization

We may use action filters for authorization in an ASP.NET MVC application. An action filter is an attribute that we can apply to a controller action or an entire controller. The ASP.NET MVC framework includes several action filters including OutputCache, HandleError, and Authorize. Authorize action filter enables us to restrict access to a particular user or role. Authorization filters implements the IAuthorizationFilter attribute. Authorization filters are used to implement authentication and authorization for controller actions. The ASP.NET MVC framework includes a base ActionFilterAttribute class. This class implements both the IActionFilter and IResultFilter interfaces and inherits from the Filter class. The base ActionFilterAttribute class has the following methods that to override: OnActionExecuting – called before a controller action is executed. OnActionExecuted – called after a controller action is executed. OnResultExecuting – called before a cont...