Category Archives

5 Articles
Most useful mocking tricks for MOQ and C#

Most useful mocking tricks for MOQ and C#

The following list is my most used mocking techniques to create, setup and verifies injected dependencies.

Create

This section lists all the most used ways of creating a mocked object of a dependancy

Create a mocked object of a dependency

var mockedSystemUnderTest = new Mock();

Setup

This section lists all the most used ways of setting up behaviors of a dependancy

Setup all properties in a mocked dependency

mockedSystemUnderTest.SetAllProperties();

Setup a specific property in a dependency

mockedSystemUnderTest.SetGet(prop=>prop.SomeProperty).Returns("SomeValue");

Read More

Secure Web API RESTful services using Bearer tokens

Secure Web API RESTful services using Bearer tokens

This article talk s about securing ASP.net Restful web services, this is the first article of the series “Securing asp.net web applications”. This article talks about implementing Bearer Token-based Authentication and Authorization.

Authentication

Is the process of identifying the user and validating their credentials. If we map the Authentication to the example in this article, the process of validating user credentials and generating an access token is called Authorization.

Authorization

Is the process of determining if the user can access the requested resource. If we map the Authentication to the example in this article, the process of reading given Authorization token and determining if the user has access to requested Web API controller/ action is called Authorization.

Whats wrong with cookies? Why do we need token based authorization?

There is nothing wrong with cookies. Cookies are only usable in browser-based web applications. The RESTful web services (Web API) can be used on every platform that has an internet connection. Cookies might not be the right fit for those platforms (e.g. mobile applications). Embedding information in an interchangeable data format is far better than using Cookies to store user information.

Step 1. Create an Asp.Net Web API application

Read More

Comparing Asp.Net core middleware with Http Modules and Handlers

Http Modules & Http Handlers

1. In the older version of Asp.Net, the Http modules hooked into Application lifecycle events to provide reusable services.

2. In the older version of Asp.Net, the Http Handlers were responsible for Generating responses for Http requests.

3. Order of execution of Http modules and Http Handlers are driven by Asp.net life cycle events

Asp.Net core middleware

1. generates responses for requests.

2. Provides application level services and features.

3. Order of execution of Asp.Net core middleware are driven by the order it is added into middleware pipeline.

4. Middlewares are available in Asp.Net web api (.Net 4.5) and .Net core

ASP.Net Core MVC request life cycle – Middleware Pipeline

Middleware Pipeline

ASP Net Core MVC is built on top of a concept called Middleware. Middleware pipeline is a chain of modular components that provide essential building block of how your application handles HTTP request. These components are generating a response for an incoming request directly or with help of a framework such as MVC.

Features like Routing, sessions, CORS, Authentication, Caching etc are implemented as middleware.

Example of asp net core middleware pipeline

Read More

The type or namespace name system could not be found Error in C#

How to resolve “The type or namespace name system could not be found” and “Predefined type system object is not defined or imported” errors in a .Net project c#?

Install the latest version of System.Runtime Nuget package or add a reference to System.Runtime from “Add references”.

Clean and rebuild the project once Reference is added.

PM> Install-Package System.Runtime


Read More