Posts

6 Ways to use Code Comments effectively

When you are learning to code it almost always follows the path of spaghetti-code. But, there is one magic bean you can use in your code which will make your life easier when you look back at your own code (or someone elses') 10 years later. 
"// Comments" I haven't been coding for 10+ years but all these years that I've looked at or got a chance to work on someone else's code, proper meaningful comments made my job way more productive than any other tool that I've come across. Because, good comments tell us WHY the code is that way. While the code itself tells me what it is doing.
Following are sort of best practices/guidelines for good comments. If your code is self explanatory, by all means, forget about commenting. But, if there is at least one shady magical piece of work that your code is doing, you might want to think about adding proper comments on them. Please note that this is by no means a full list of guidelines but rather a short and sweet ver…

How to create a .NET Core MVC File Uploader Module - Part 1

Image
Fine UploaderEver since I started developing websites, file/image uploading really gave me a hard time because I just couldn't figure out how to make the upload more generic and pluggable. After seeing Google/Facebook/500px♥ does it, I figured, it must be possible and should be simple. I always tend to over-complicate stuff. A prime example is this SO question (Adjusting dates in an array based on a start date) I asked a few days back. After a few tries, I came up with a solution. This is done using .NET Core MVC6 and You are free to steal this and modify according to your needs. (Give credits where necessary, be human)


So my requirements are simple:
To be able to upload files into a central file repositoryTo be able to select uploaded files and attach them to a postIf it's an image, show a previewTo be able to add metadata related to the file as we upload themTo be able to validate the said metadata before uploadingTo be able to plug it into any page without too much configura…

Token Authentication C# Console Application - Sample

How to use token authentication from a console application to call and Web API endpoint.

ASP.NET MVC Fine Grained Identity & Access Control - Part 3

In this series I will explain how I designed a generic claims based access control system ASP.NET MVC 5 with Identity 2.0.
In Part 1, I describe my approach and the initial database design. ASP.NET MVC Fine Grained Identity & Access Control - Part 1

In Part 2, I started laying foundation for the whole system with all the data models and a functional UI.

In this Part 3, I will start with most of the coding to make it work.

So far completed coding is in the GitHub repo: https://github.com/dirnthelord/ClaimsAuth

Note that, in part 2, I've mentioned the functionalities I want the system to have. In this post, I will complete all the core functionality so that I can directly edit the DB table values (using SSMS - SQL Server Management Studio) to test things out. I still need to create a UI for the super admins to easily manage permissions.

First off, lets grab all the Areas, Controllers and Actions in the system. These are what you going to lock down to certain groups.

I have po…

ASP.NET MVC Fine Grained Identity & Access Control - Part 2

Image
In this series I will explain how I designed a generic claims based access control system ASP.NET MVC 5 with Identity 2.0. In Part 1, I describe my approach and the initial database design. ASP.NET MVC Fine Grained Identity & Access Control - Part 1

In this Part 2, I will start laying foundation for the whole system with all the data models and a functional UI.



So far completed coding is in the GitHub repo: https://github.com/dirnthelord/ClaimsAuth

First, I need to decide what functionality the UI should have.
As a super admin, I need to be able to view the application resources from the UI. Add a description and search them.As a super admin, I need to be able to manage all the users in the system.As a super admin, I need to be able to manage global permissionsAs a super admin, I need to be able to manage user groups and group permissionsAs a super admin, I need to be able to manage user groups and group permissions
So, these are the list of things I need to be able to do withi…

Why You Should Always Use Decorator Pattern

Image
Life get easier when you have more reusable decorators and a good dependency injector.

Years before I choose this programming/engineering career, I always wondered, how can I make something better? How can I make it work faster or efficiently? I'm no backyard scientist or whatever so basically, it was all just thinking and speculating. Running simulations in my head, kinda. Once I started programming, I was interested in making machines talk. "Knight Rider" was my inspiration. Talking cars!!! And then, I was super interested in learning all the mechanisms behind AI. I'm a fan of AI, taking over the world or not, I always wanted to build one. For some fucked up reason, I still can't even get a simple neural-net working. Sure, copy pasting works but what's the point. I suck at math, may be that's the reason. And, as with everything in life, we come up with excuses to either postpone something or completely give up and look the other way. I just never, comp…

Custom Error Messages in ASP.NET MVC

Image
Even when you have custom errors mode on,
and Have all the custom error pages setup, like the following..

<customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRewrite"> <error statusCode="403" redirect="~/403" /> <error statusCode="401" redirect="~/401" /> <error statusCode="404" redirect="~/404" /> <error statusCode="409" redirect="~/409" /> <error statusCode="500" redirect="~/500" /> </customErrors>

You even set Response.TrySkipIisCustomErrors = true; on your error handler to get rid of those ugly IIS default error pages.
/// <summary> /// Returns a Not Found error. /// </summary> [HttpGet, Route("404")] [AllowAnonymous] public ActionResult NotFound() { Log.E…