Skip to content

Instantly share code, notes, and snippets.

View jamescrowley's full-sized avatar

James Crowley jamescrowley

View GitHub Profile
@jamescrowley
jamescrowley / ndepend.fsx
Created April 22, 2014 21:15
NDepend F# make
module Fake.NDepend
open Fake
open System
open System.IO
open System.Text
let getWorkingDir workingDir =
Seq.find isNotNullOrEmpty [workingDir; environVar("teamcity.build.workingDir"); "."]
|> Path.GetFullPath
@jamescrowley
jamescrowley / AntiForgeryTokenFilter.cs
Last active August 29, 2015 14:01
Applying anti forgery tokens globally
public class AntiForgeryTokenFilter : IAuthorizationFilter
{
private readonly AcceptVerbsAttribute _verbs;
public AntiForgeryTokenFilter(HttpVerbs verbs)
{
_verbs = new AcceptVerbsAttribute(verbs);
}
public void OnAuthorization(AuthorizationContext filterContext)
{
if (_verbs.IsValidForRequest(filterContext, null))
@jamescrowley
jamescrowley / JsonValidatingModelBinder.cs
Last active September 29, 2019 09:48
Validating JSON with ASP.NET request validation
public class JsonValidatingModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = base.BindModel(controllerContext, bindingContext);
if (!IsJsonRequest(controllerContext))
{
return result;
}
if (!bindingContext.ModelMetadata.RequestValidationEnabled)
@jamescrowley
jamescrowley / StatelessAuth.cs
Created May 24, 2014 11:02
Owin Stateless auth
using System.Security.Claims;
using Microsoft.Owin;
namespace Owin.StatelessAuth
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class StatelessAuth : OwinMiddleware
@jamescrowley
jamescrowley / Maybe.cs
Created May 24, 2014 11:16
Maybe from Lokad
// Imported from Lokad.Shared, 2011-02-08
using System;
namespace Lokad.Cloud.Storage
{
/// <summary>
/// Helper class that indicates nullable value in a good-citizenship code
/// </summary>
/// <typeparam name="T">underlying type</typeparam>
@jamescrowley
jamescrowley / 01-Startup.cs
Last active July 12, 2016 21:59
Global OWIN handling of errors & page not found
public class Startup
{
public void Configuration(IAppBuilder app)
{
app
.Use<StatusCodeFromExceptionMiddleware>()
.UseNancy();
}
}
@jamescrowley
jamescrowley / GlobalExceptionHandlingMiddleware.cs
Last active August 29, 2015 14:02
GlobalExceptionHandlingMiddleware
public class GlobalExceptionHandlingMiddleware : OwinMiddleware
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public GlobalExceptionHandlingMiddleware(OwinMiddleware next) : base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
try
@jamescrowley
jamescrowley / ErrorHandlingTests.cs
Created June 16, 2014 12:13
ErrorHandlingTests
namespace FundApps.DocumentGenerator.FeatureTests.OwinMiddleware
{
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FundApps.DocumentGenerator.Web.Infrastructure.Owin;
@jamescrowley
jamescrowley / web.config
Created June 25, 2014 16:44
Owin/IIS OPTIONS problem
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="owin:HandleAllRequests" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
@jamescrowley
jamescrowley / DoesntWork.fsx
Created July 16, 2014 10:17
PowerShell / F# Make weirdness
Target "PowerShell" (fun _ ->
let result = ExecProcess (fun info ->
info.FileName <- "powershell.exe"
info.WorkingDirectory <- (currentDirectory @@ "deploy")
info.Arguments <- "-ExecutionPolicy RemoteSigned -Command \"Import-Module Carbon\"") System.TimeSpan.MaxValue
if result <> 0 then failwithf "Error deploying local"
)