Skip to content

Instantly share code, notes, and snippets.

View nikanos's full-sized avatar

Nikanos Polykarpou nikanos

  • ::1, Nicosia, Cyprus
View GitHub Profile
@nikanos
nikanos / WebApiMockResponse.linq
Last active May 1, 2024 19:16
LINQPad 5 (.NET Framework) Query to mock API Responses
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Windows.Forms.dll</Reference>
<NuGetReference>Microsoft.AspNet.WebApi.OwinSelfHost</NuGetReference>
<Namespace>Microsoft.Owin.Hosting</Namespace>
<Namespace>Owin</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>System.Web.Http</Namespace>
@nikanos
nikanos / Create-SelfSignedCert.ps1
Last active April 8, 2024 07:55
Powershell script example to create a self-signed certificate valid for 10 years and stored in Personal store of LocalMachine
# Create a 10-year self-signed certificate for www.abc.cde domain and store it in Personal store of Computer (LocalMachine)
# The command needs to run from an elevated powershell window
New-SelfSignedCertificate -DnsName www.abc.cde -NotAfter (Get-Date).AddYears(10) -CertStoreLocation cert:\LocalMachine\My
@nikanos
nikanos / MachineKeyProtector.cs
Created April 18, 2023 19:07
.NET Framework WebApi Bearer Token Decode
public class MachineKeyProtector : IDataProtector
{
private readonly string[] _purpose =
{
typeof(OAuthAuthorizationServerMiddleware).Namespace,
"Access_Token",
"v1"
};
public byte[] Protect(byte[] userData)
@nikanos
nikanos / RemoveEmptyFolders.ps1
Created March 7, 2023 09:47
Powershell script to remove empty folders under the current directory
Get-ChildItem -Recurse .|where {$_.PSISContainer -and @($_ | Get-ChildItem).Count -eq 0 }|Remove-Item
@nikanos
nikanos / StringUtils.cs
Created January 3, 2023 20:19
C# string SafeFormat method - used as a wrapper to string.Format() and passing Undefined to any missing arguments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class StringUtils
{
public static string SafeFormat(string format, params object[] parameters)
{
const int MAX_NUMBER_OF_UNPASSED_PARAMETERS = 50;
@nikanos
nikanos / Animal.c
Created December 4, 2022 21:43
Inheritance example using C
#include <stdio.h>
#include <string.h>
#include "Animal.h"
void Animal_Ctor(Animal* pa)
{
pa->pfspeak=Animal_Speaking;
}
void Animal_Speaking(Animal* pa)
@nikanos
nikanos / shellforkex1.cpp
Created December 4, 2022 21:30
linux C++ shell example using fork()
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
@nikanos
nikanos / forkex1.c
Last active December 4, 2022 21:12
linux fork() C example - parent process starts a child process that sleeps for 3 seconds and either waits or terminates it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int pid;
int status;
@nikanos
nikanos / tsql_loop.sql
Last active November 29, 2022 17:07
SQL Server TSQL Loop example
DECLARE @cnt INT = 0;
WHILE @cnt < 10
BEGIN
PRINT 'Doing Something in loop #' + Convert(VARCHAR(10),@cnt);
--Use RAISEERROR to flush PRINT buffer
RAISERROR(N'', --message
0, --severity
1) --state
WITH NOWAIT;
@nikanos
nikanos / Ensure.cs
Created November 10, 2022 21:24
C# class for method parameter checks
public static class Ensure
{
public static void ArgumentNotNull<T>(T argument, string argumentName) where T : class
{
if (argument == null)
throw new ArgumentNullException(argumentName);
}
public static void ArgumentNotNull<T>(Nullable<T> argument, string argumentName) where T : struct
{