Skip to main content

Posts

Showing posts from 2017

A cookie wrapper class. Easily read, create, update and delete cookies in MVC

Create a new class with name CookieHelper.cs In your project and paste the following code. public class CookieHelper { #region Constants /// <summary> /// The cookie name /// </summary> public const string cookieName = "UserName"; #endregion #region Enums public enum EnumCookieInterval { SECOND = 0, MINUTE = 1, DAY = 2, MONTH = 3, YEAR = 4 }; #endregion #region Utilities /// <summary> /// Calculates the cookie lifetime. /// </summary> /// <param name="duration">The duration.</param> /// <param name="durationType">Type of the duration.Use enum to specify</param> /// <returns>Expire date for cookie</returns> private static DateTime CalculateCookieLifetime(int duration, Enum

A Helper Function Which Verifies An Uploading File is in Valid Image Format in MVC

In almost 90% of projects, we need to upload images to server and store them. In most cases, hackers try to exploit an image upload system and try to upload exploitable materials like webshells, some harmful scripts, table deletions scripts, etc. To prevent this, I have written one helper function which validates file in many conditions and makes sure the file is in correct image format. The code is not fully written by me, I researched many articles and filtered the conditions which helps us to validate the required output. /// <summary> /// Verifies that a uploading file is in valid Image format /// </summary> /// <author> /// Mayur Lohite /// </author> /// <param name="postedFile">File which is selected for upload</param> /// <param name="imageMinBytes">Minimum file size in byte</param> /// <param name="imageMaxBytes">