Wiring up a custom authentication method with OWIN in Web API Part 5: abstracting away the auth logic

Introduction

In the previous post we built the necessary OWIN components for our custom authentication logic: the middleware and the extension method that’s attached to the IAppBuilder object. We also diverged a little and discussed how to use the Use extension method to inject the necessary objects into the constructor of the middleware class.

In this post we’ll extend PinAuthenticationHandler so that the actual authentication logic can be easily changed.

Read more of this post

Python language basics 68: iterating over a dictionary with dictionary comprehension in Python

Introduction

In the previous post we looked at a Python language construct called comprehension. We saw that comprehensions are a very concise way of iterating over a collection, applying a function to each element and adding the resulting element to a new collection. All that is declared in a single comprehension statement.

In this post we’ll look at dictionary comprehensions.

Read more of this post

Python language basics 67: iterating over a list with list comprehension

Introduction

In the previous post we looked at a practical example using the yield keyword in Python. The function distributed an integer across a number of groups as equally as possible.

In this post we’ll look at an interesting language construct called comprehension. A comprehension in Python is a form of iteration which provides a concise and expressive way of iterating over a collection. In particular we’ll consider list comprehensions, but keep in mind that comprehensions can be applied to any iterable object, such as sets, tuples or generator functions. The delimiting element, such as curly braces for sets and square brackets for lists will determine the type of object returned by the comprehension.

Read more of this post

5 ways to concatenate strings with C# .NET

There are multiple ways to build a string out of other strings in .NET. Here come 5 of them.

Let’s start with the most obvious one that language learners encounter first, i.e. concatenation done by the ‘+’ operator:

string concatenatedOne = "This " + "is " + "a " + "concatenated " + "string.";

Read more of this post

Wiring up a custom authentication method with OWIN in Web API Part 4: putting the components to work

Introduction

In the previous post we built two important components around the custom authentication logic. First off, we wrote a very simple implementation of the abstract AuthenticationOptions class where we only declared the name of the authentication mode. You can anyway add any extra property and overloaded constructors as you wish to this class. Then we built the custom authentication handler, i.e. PinAuthenticationHandler which derives from the abstract AuthenticationHandler class.

In this post we’ll add a couple more components specifically to add our authentication handler to the OWIN chain. We’ll also test the authentication logic.

Read more of this post

How to hide the key pressed in a .NET console application

You probably know how to read the key pressed by the user in a .NET console application by the Console.ReadKey() method:

ConsoleKeyInfo pressedKey = Console.ReadKey();
Console.WriteLine("You have pressed: {0}", pressedKey.Key);

Read more of this post

Implementing an indexer for your object with C# .NET

Indexers are used extensively when accessing items in an array or List:

Friend f = friends[2];

It’s fairly easy to implement your own indexer. Imagine a table with guests sitting around. We could implement an indexer to easily access guest #n.

The Guest object is simple with only one property:

public class Guest
{
	public string Name { get; set; }
}

Read more of this post

Wiring up a custom authentication method with OWIN in Web API Part 3: the components

Introduction

In the previous post we looked at HTTP headers in code. We saw how to collect the headers from a request and a response and how to check for the presence of a certain header type. We also built a simple tester C# console application that sends a web request to our Web API controller and sets the necessary custom authentication header.

In this post we’ll start building the components necessary for the OWIN middleware.

Read more of this post

Python language basics 66: a practical example of a yield generator in Python

Introduction

In the previous post we continued our investigation of yield generators in Python. In particular we looked at how a yield function could remember the state of its internal variables. We saw how code execution jumps in and out of a yield function as it’s being enumerated.

At this point you may think that yield functions are interesting but what can they be used for? You’ll find a lot of examples on the internet. In this post we’ll look at the following scenario:

Say you’d like to divide an integer, e.g. 20, into equal parts of 3 and distribute any remainder equally across the groups. The result of such an operation would be the following 3 integers:

7,7,6

20 can be divided into 3 equal parts of 6 and we have a remainder of 20 – 6 * 3 = 2. 2 is then added as 1 and 1 to the first two groups of 6. The result is a more or less equal distribution of the start integer.

The following function will perform just that:

Read more of this post

Python language basics 65: keeping state in yield generators

Introduction

In the previous post we looked at a basic example of using the yield keyword in Python. We saw that yield generator functions are essentially collections that return a certain number of elements when they are enumerated. The elements to be returned – yielded – are not loaded into memory before they are required in a loop. We also said that generator functions can “remember” the state of their variables.

We’ll see an example of what this means in code.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.