Posts

Showing posts from January, 2016

validate uploaded image wrt size, format in asp.net mvc5

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Model argModel, HttpPostedFileBase file) {    string msg = Utilities.IsImageValid(file);    if (!msg.Trim().Equals("ok"))    {       ModelState.AddModelError("", msg);    }    if (ModelState.IsValid)    {         //saving here         return View("Index");     }     return View("Create", argModel); } public class Utilities     {         public static string IsImageValid(object value)         {             var file = value as HttpPostedFileBase;             if (file == null)             {                 return "Please attach photograph.";             }             if (file.ContentLength > 1 * 300 * 300)             {                 return "Uploaded file size should be less than 1 MB";             }             try             {                 using (var img = Image.FromStream(file.InputStream))                 {          

form authentication using custom principle in asp.net mvc

Here's how I do it. I decided to use IPrincipal instead of IIdentity because it means I don't have to implement both IIdentity and IPrincipal. Create the interface interface ICustomPrincipal : IPrincipal { int Id { get ; set ; } string FirstName { get ; set ; } string LastName { get ; set ; } } CustomPrincipal public class CustomPrincipal : ICustomPrincipal { public IIdentity Identity { get ; private set ; } public bool IsInRole ( string role ) { return false ; } public CustomPrincipal ( string email ) { this . Identity = new GenericIdentity ( email ); } public int Id { get ; set ; } public string FirstName { get ; set ; } public string LastName { get ; set ; } } CustomPrincipalSerializeModel - for serializing custom information into userdata field in FormsAuthenticationTicket object. public class CustomPrincipalSerializeModel {