↓ Skip to main content

C#

Cryptography .NET, Avoiding Timing Attack

Cryptography .NET, Avoiding Timing Attack

·3 mins
When comparing two MACs or hashes (also password hashes) for equality you would first think a simple comparison would be okay? Think again as they are susceptible to timing attacks. In cryptography, a timing attack is a side channel attack in which the attacker attempts to compromise a cryptosystem by analysing the time taken to execute cryptographic algorithms.
SecureString

SecureString

·7 mins
I’ve come across a class in the System.Security namespace that is often overlooked. The class is SecureString. In this post, I will go over what SecureString is and why it is needed. Have you ever come across these scenarios before?
Raising Multiple Exceptions with AggregateException

Raising Multiple Exceptions with AggregateException

·6 mins
There are occasions where you are aware of many exceptions within your code that you want to raise together in one go. Perhaps your system makes a service call to a middleware orchestration component that potentially returns many exceptions detailed in its response, or another scenario might be a batch processing task dealing with many items in one process that require you to collate all exceptions until the end and then throw them together.
Using MongoDB

Using MongoDB

·1 min
If you want to setup MongoDB with C#, then the best option is to follow the getting started guide on the MongoDB website https://docs.mongodb.com/getting-started/csharp/client/ You can install the NuGet package for MongoDB by typing into the Package Manager Console
Custom Configuration in your config

Custom Configuration in your config

·2 mins
This post is to show how to setup a customised configuration section in your app.config or web.conifg of your .NET application I have written an article on this before, back in 2008, but things have moved on in the libraries and this is the latest and easiest way to create your own customised configuration.

New Features in C# 7.0

·7 mins
Microsoft have release C# 7.0 and it comes with a number of new features and brings a focus on data consumption, code simplification and performance. Tuples have been around for a while but they have made them so much easier to use, which make it easy to have multiple results, and pattern matching which simplifies code that is conditional on the shape of data. But there are many other features big and small.
How slow are Exceptions

How slow are Exceptions

·4 mins
I’ve seen from time to time applications that wrap a TRY – CATCH around some code, but do nothing except carry on. So why is this so wrong? As programmers we want to write quality code that solves problems. Unfortunately, exceptions come as side effects of our code. No one likes side effects, so we soon find our own ways to get around them. I have seen some smart programmers deal with exceptions the following way:
Entity Framework Unit of Work Patterns

Entity Framework Unit of Work Patterns

·15 mins
I’m not often shocked, but when I found out that Entity Framework was not thread safe, I was horrified. I even had to buy a fellow college dinner as I lost a bet. Now that I knew what the problem was how can it be resolved?
Decorator Pattern

Decorator Pattern

·5 mins
Decorator pattern falls under Structural Pattern of Gang of Four (GOF) Design Patterns in .Net. Decorator pattern is used to add new functionality to an existing object without changing its structure. Hence Decorator pattern provides an alternative way to inheritance for modifying the behavior of an object. In this article, I would like to go over what the decorator pattern can do, and how it works.

Kendo Grid custom sort order

·2 mins
I’ve been racking my brains as the sorting on the Kendo Grid is pretty good, only that I need to be able to not sort in the order that is being displayed. An example would be numbers written out in full and you want them in number sort order.
Code Metrics what do they mean?

Code Metrics what do they mean?

·2 mins
 Code metrics is a set of software measures that provide developers better insight into the code they are developing. By taking advantage of code metrics, developers can understand which types and/or methods should be reworked or more thoroughly tested.
IoC Benchmark

IoC Benchmark

·1 min
So which is the fastest at different IoC Lifestyles. In this benchmark we will be running, head to had, with Castle Windsor, Ninject, Spring.NET, Unity, Microsoft DI, AutoFac, and Structure Map. Here is a working project, this uses NuGet to included the packages, so as time goes on you may find the packages go out of date, so you’ll need to refactor the code to work with the next packages.
Factory Support Facility

Factory Support Facility

·3 mins
Factory Support Facility allows using factories to create components. This is beneficial when you want to make available as services components that do not have accessible constructor, or that you don’t instantiate, like HttpContext. Prefer UsingFactoryMethod over this facility: while the facility provides programmatic API it is deprecated and its usage is discouraged and won’t be discussed here. Recommended approach is to use UsingFactoryMethod method of fluent registration API to create components. This limits the usefulness of the facility to XML-driven and legacy scenarios.
Registering components for IoC

Registering components for IoC

·4 mins
Inversion of Control (IoC) is quite a big step for some developers, but the benefits once implemented are huge. With the applications performance, maintainability, unit testing and more importantly separation of concerns, just to name a few.

Introduction to IoC with Windsor

·7 mins
I do love dependency injection, but many developers still seem to miss the point and the reason for having it. If you’re only using it on a small scale, you don’t really need any tools to use the technique. But once you’re used to this design technique, you’ll quickly start using it in many places of your code. If you do, it quickly becomes cumbersome to deal with the real instances of your runtime dependencies manually. This is where tools like Inversion Of Control (IoC) containers come in to play. There are a few solid containers available for the .NET world, and even Microsoft has released their own container. Basically, what the IoC container does for you, is take care of providing dependencies to components in a flexible and customizable way. It allows clients to remain completely oblivious to the dependencies of components they use. This makes it easy to change components without having to modify client code. Not to mention the fact that your components are a lot easier to test, since you can simply inject fake dependencies during your tests.

How to Rank your search results with multiple search terms using LINQ and EntityFramework

·2 mins
I have always used a ranked search criteria, it’s the only true way to get good results back from a data set. But I’ve been doing my ranking within C# code. But this got all the data from the data source and then ranked the results. This is very poor on performance, as we should only be returning back the results for the page size we require.

Make Fake MVC Objects for testing

·3 mins
If you need to create some Fake MVC objects for testing MVC objects then here you are: FakeControllerContext using System.Collections.Specialized; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.SessionState; using MvcContrib.TestHelper.Fakes; public class FakeControllerContext : ControllerContext { public FakeControllerContext(IController controller) : this(controller, null, null, null, null, null, null) { } public FakeControllerContext(IController controller, HttpCookieCollection cookies) : this(controller, null, null, null, null, cookies, null) { } public FakeControllerContext(IController controller, SessionStateItemCollection sessionItems) : this(controller, null, null, null, null, null, sessionItems) { } public FakeControllerContext(IController controller, NameValueCollection formParams) : this(controller, null, null, formParams, null, null, null) { } public FakeControllerContext(IController controller, NameValueCollection formParams, NameValueCollection queryStringParams) : this(controller, null, null, formParams, queryStringParams, null, null) { } public FakeControllerContext(IController controller, string userName) : this(controller, userName, null, null, null, null, null) { } public FakeControllerContext(IController controller, string userName, string[] roles) : this(controller, userName, roles, null, null, null, null) { } public FakeControllerContext ( IController controller, string userName, string[] roles, NameValueCollection formParams, NameValueCollection queryStringParams, HttpCookieCollection cookies, SessionStateItemCollection sessionItems ) : base(new FakeHttpContext(new FakePrincipal(new FakeIdentity(userName), roles), formParams, queryStringParams, cookies, sessionItems), new RouteData(), (Controller)controller) { } }

Disclaimer Page using MVC

·2 mins
If you ever need a disclaimer page in MVC, there are a few things you need to consider, the first is you should not be able to get to a page without agreeing to the disclaimer and if you have to re-login then you need to be presented with the disclaimer page again.