Skip to content

Instantly share code, notes, and snippets.

@pmhsfelix
pmhsfelix / gist:3949044
Created October 24, 2012 21:32
Canonicalized Header and Resource String
private static string[] GetCanonicalizedHeaders(HttpRequestHeaders headers)
{
return headers
.Where(p => p.Key.StartsWith("x-ms-", StringComparison.InvariantCultureIgnoreCase))
.Select(p => new { Name = p.Key.ToLower(), Value = p.Value.First() })
.OrderBy(p => p.Name)
.Select(p => string.Format("{0}:{1}", p.Name, p.Value))
.ToArray();
}
@pmhsfelix
pmhsfelix / oauth2demo.js
Created November 16, 2012 18:10
OAuth 2 Client demo using Node.js
(function(){
var querystring = require('querystring');
var url = require('url');
var request = require('request');
var http = require('http');
var util = require('util');
var child_process = require('child_process');
var config = {
@pmhsfelix
pmhsfelix / gist:4151369
Created November 26, 2012 23:33
Generating and validating JWT tokens using JWTSecurityTokenHandler
[Fact]
public void First()
{
var tokenHandler = new JWTSecurityTokenHandler();
var symmetricKey = GetRandomBytes(256/8);
var now = DateTime.UtcNow;
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace DownloadSslCert
{
@pmhsfelix
pmhsfelix / gist:5227995
Created March 23, 2013 14:53
Naive HTTP intermediary using Web API with self-host
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.ServiceModel;
@pmhsfelix
pmhsfelix / gist:5667375
Created May 29, 2013 01:29
A ASP.NET Web API based local proxy that forwards via Runscope, using @darrelmiller RunscopeMessageHandler. Just configure your HTTP client (e.g. a browser) to use a proxy at 127.0.01:8080
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.ServiceModel;
[Fact]
public async Task Fact()
{
var res = await Tester.Run(
config =>
{
config.Filters.Add(new BasicAuthenticationFilter("myrealm", _validator));
},
() =>
{
public class BasicAuthenticationFilterTests
{
private Func<string, string, Task<IPrincipal>> _validator = (username, password) =>
{
var princ = username == password ? new ClaimsPrincipal(new GenericIdentity(username)) : null;
return Task.FromResult(princ as IPrincipal);
};
[Fact]
public async Task Correctly_authenticated_request_has_a_valid_User()
[Fact]
public async Task Fact()
{
await OwinTester.Run(
useConfiguration: app =>
{
app.UseBasicAuthentication(Options);
},
useRequest: () =>
{
@pmhsfelix
pmhsfelix / gist:6148160
Last active November 22, 2016 16:25
Katana based HTTP Basic Authentication middleware, mostly for learning purposes
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Infrastructure;
using Owin;