Skip to content

Instantly share code, notes, and snippets.

@benfoster
benfoster / gist:8843578
Created February 6, 2014 12:50
Tenant Resolver API
// 1. Return identifiers using out parameter
public interface ITenantResolver<TTenant>
{
Task<TTenant> Resolve(string identifier, out string[] tenantIdentifiers);
}
// 2. Return ResolvedTenant object containing identifiers
public interface ITenantResolver<TTenant>
@benfoster
benfoster / gist:8889154
Created February 8, 2014 19:48
jQuery Fitframe
/*!
* jQuery fitframe plugin
* Further changes, comments: @benfosterdev
* Licensed under the MIT license
*/
;(function ( $, window, document, undefined ) {
var pluginName = 'fitframe',
defaults = {
@benfoster
benfoster / gist:9009407
Created February 14, 2014 21:14
Simple pipeline example
using NUnit.Framework;
namespace PipelineDemo
{
public abstract class Handler<T>
{
protected Handler<T> next;
public void SetInnerHandler(Handler<T> handler)
{
@benfoster
benfoster / gist:9091425
Last active August 29, 2015 13:56
An abuse of OWIN?

I originally thought about writing this as a blog post but it's more of a discussion point than anything else - plus Pete Smith (Roysvork) blogged about it yesterday - http://roysvork.wordpress.com/2014/02/18/are-we-gowing-in-the-right-direction/.

I arrived pretty late to the OWIN party and I'll admit I didn't see the benefit at first. OWIN states its goal as decoupling server and application. So now I can host a website in a windows service - that's pretty cool, although in reality it's unlikely I'll ever need to do this.

However, as I started to learn more about OWIN and see what Microsoft were doing (pushing more of their libraries up to the OWIN level) I could see the opportunities that OWIN opened up. If I write a library that does something in the request pipeline, I could write this as an OWIN Middleware Component (OMC) and now it would work regardless of the web framework you were using (providing it was OWIN compatible) - Web API, Nancy, Simple.Web etc.

A few weeks ago I blogged about a project

@benfoster
benfoster / gist:9269673
Created February 28, 2014 11:43
srcset
<img src="images/kimbra.jpg"
srcset="images/kimbra-480.jpg 480w, images/kimbra-768.jpg 768w, images/kimbra.jpg 1x"
alt="" />
@benfoster
benfoster / gist:9409094
Last active August 29, 2015 13:57
Refreshing claims using Federated Identity Session Authentication Module (SAM)
public async Task RefreshClaimsAsync()
{
SessionSecurityToken token;
if (FederatedAuthentication.SessionAuthenticationModule.TryReadSessionTokenFromCookie(out token))
{
// obtain their API access token using current identity
var accessToken = await tokenManager.GetTokenFromStoreAsync(token.ClaimsPrincipal.Identity.Name);
// Reload the claims
await WriteSessionToken(accessToken);
}
@benfoster
benfoster / gist:9412542
Created March 7, 2014 14:35
Refreshing claims using ASP.NET Identity Cooke based AuthN
public static class AuthenticationManagerExtensions
{
public static void RefreshUserClaims(
this IAuthenticationManager authManager,
IEnumerable<Claim> updatedClaims,
string authenticationType)
{
var identity = new ClaimsIdentity(updatedClaims, authenticationType);
authManager.SignOut(authenticationType);
authManager.SignIn(identity);
@benfoster
benfoster / gist:9543251
Created March 14, 2014 07:05
File upload service with node.js
var http = require('http'),
fs = require('fs');
var server = http.createServer(function(request, response) {
var file = fs.createWriteStream('upload.jpg'),
fileBytes = request.headers['content-length'],
uploadedBytes = 0;
request.pipe(file);
@benfoster
benfoster / gist:9741613
Created March 24, 2014 14:51
Sending Subscription Notifications
/// <summary>
/// Determines whether a notification is due in the given <param name="period"/>.
/// </summary>
/// <param name="period">The notification period to check.</param>
/// <returns>True if a notification is due, otherwise False.</returns>
public bool IsNotificationDue(TimeSpan period)
{
Ensure.Argument.NotNull(period, "period");
var periodStart = NextRenewalDate - period;
public class root_mapper : base_context
{
static object result;
Establish ctx = ()
=> new RootMapper<PostsController>(x => x.Index()).RegisterRoutes(routes);
Because of = ()
=> result = "~/".WithMethod(HttpVerbs.Get);