Friday, February 23, 2018

jwt-decode - Decode a JWT token in your browser

During the preparation for a training I found the following great utility library; jwt-decode.

jwt-decode is a small browser library that helps decoding JWTs token which are Base64Url encoded.

Installation

I installed the library using

npm install jwt-decode

Usage

As I’m using webpack I was able to include the package using a require

var jwtDecode = require('jwt-decode');
Next step is to take the token and call jwtDecode:
var token = 'eyJ0eXAiO.../// jwt token';

var decoded = jwt_decode(token);
console.log(decoded);
  

Thursday, February 22, 2018

Angular error: Cannot find module '@angular-devkit/core'

Yesterday a colleague asked me for help with the following problem. Angular CLI was working fine on his system(he was using version 1.6.4 on his system), but when he tried to run an Angular project created using an earlier Angular CLI version, he got the following error:

Cannot find module '@angular-devkit/core'

We found 2 solutions that both seemed to work.

Solution 1

Install the @angular-devkit/core package explicitly using 

npm i -D @angular-devkit/core

Solution 2

Upgrade the angular-cli version using

npm update -g @angular/cli

Wednesday, February 21, 2018

Log Parser Studio

The last 2 weeks we had some production issues where an unexpected 401 message was returned from our IIS servers. To further investigate the issue, I got an old tool out of the dust; Log Parser Studio.

Log Parser Studio is a utility that allows you to search through and create reports from your IIS, Event, EXADB and others types of logs. It builds on top of Log Parser 2.2 and has a full user interface for easy creation and management of related SQL queries.

Log Parser Studio allows to query all kinds of log files using a SQL like syntax. It comes out of the box with a large list of sample queries that can help get started.

image

How to use it

  • Open Log Parser Studio(LPS.exe)
  • Click on the New Query button or choose one of the queries from the library

 image

  • A new query window appears

image

  • Now it is time to first specify the LogType. Click on the Log Type link in the middle and choose the correct type from the list:

image

  • Next we have to select the files to query. Click on the Choose Files/folders icon

image

  • The Log File Manager window appears. Here you can use Add Files or Add Folders to add log files.
    • After selecting some files, click OK to return to the Main screen.

image

  • Now it is finally time to execute the query. Click on the Execute button

image

  • After some time(depending on the number of log files and the complexity of the query), the query results are returned:

image

Tuesday, February 20, 2018

ASP.NET Core - IHttpContextAccessor

ASP.NET Core introduces the IHttpContextAccessor interface as a way to provide access to the HttpContext. Before you can use it, you have to register it at application startup inside the IServicesCollection. During a code review I noticed that it was registered like this:

This turns out to be wrong. The IHttpContextAccessor should be registered as a singleton. So the correct code is the following:

More information: https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/

Monday, February 19, 2018

JSON.NET–Resolve private setters

On one of my projects we are using event sourcing as our data persistence strategy. Based on the event stream I build projections using read models.

I prefer to keep my projected read model immutable but turns out that JSON.NET fails to deserialize the state. All properties retain their default values.

A solution is to create a custom contract resolver that uses reflection to set the property values:

Don't forget to apply this contract resolver using the JsonSerializerSettings:

Thursday, February 15, 2018

PostgreSQL–Generate an auto incrementing number on year basis

For an application we are building we had the following requirement:

A unique increasing number should be generated for every document on a year by year basis. So at the end of each year the document counter should be reset to 0 and start to increase again.

As we are using PostgreSQL as our database, I decided to implement this feature using sequences.

First thing I did was creating a function that generates a new sequence based on a specific name:

This allows me to create a new sequence dynamically at the beginning of each year.

Next thing I did was creating another function that will first invoke the f_create_seq function to see if a sequence already exists and then calls this sequence to get the next number in line.

I invoke this function from my application where I pass a year as the sequence name parameter: