Skip to content

Instantly share code, notes, and snippets.

@jhoerr
jhoerr / DI
Created October 14, 2013 15:52
Three examples of how to get a dependency, FooProvider, into a class that needs it.
// Bad!
// The FooProvider is instantiated in the constructor. Very hard to test.
public class OldSchool
{
private FooProvider _fooProvider;
public OldSchool()
{
_fooProvider = new FooProvider();
}
@jhoerr
jhoerr / MachineKeyGenerator.cs
Last active August 29, 2015 14:02
Web.config machine key generator
[TestFixture]
public class MachineKeyGenerator
{
[Test]
public void GenerateMachineKey()
{
Console.Out.WriteLine(@"<machineKey validationKey=""{0}"" decryptionKey=""{1}""/>", Key(512), Key(192));
}
private static string Key(int bits)
@jhoerr
jhoerr / gist:5904fb86e18a17321710
Created August 28, 2014 13:49
A TeamCity metarunner to set .Net assembly version from Git release/tag
<?xml version="1.0" encoding="UTF-8"?>
<meta-runner name="Set Assembly Version from Git Release/Tag">
<description>Set the version of all assemblies to the value of the Git tag.</description>
<settings>
<parameters>
<param name="autoinc.path" value="%autoinc.path%" spec="text description='The file containing per-configuration, per-branch build counters. Will be created if it does not exist.' display='normal' label='Build Counter Data File Path' validationMode='not_empty'" />
</parameters>
<build-runners>
<runner name="Set Assembly Version from Git Release/Tag" type="jetbrains_powershell">
<parameters>
@jhoerr
jhoerr / gist:677fa66595c710b22599
Created June 25, 2015 22:36
Get Google service account access token for self or impersonation using private key string
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Nito.AsyncEx;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
@jhoerr
jhoerr / gist:6d3ba86e6f44a30457df
Last active July 24, 2021 12:12
Getting claims from an IPrincipal
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
namespace Common
{
public static class PrincipalExtensions
{
public static IEnumerable<string> Roles(this IPrincipal user)
public static class ConcurrentCollectionExtensions
{
private const int MaxDegreeOfConcurrency = 10;
/// <summary>
/// Concurrently perform a task on each member of a collection
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection">The collection upon which each task should be performed</param>
/// <param name="concurrentTask">The task to perform</param>
// LINQPad
void Main()
{
new Demo().Nested(1).Result.Dump();
new Demo().Continuation(1).Result.Dump();
new Demo().Variables(1).Result.Dump();
new Demo().NestedAlt(1).Result.Dump();
}
// Define other methods and classes here
// lightbulb
public static async Task<TOut> MapAsync<TIn,TOut>(this Task<TIn> @this, Func<TIn, Task<TOut>> fn) => await fn(await @this);
// all permutations
public static TOut Map<TIn,TOut>(this TIn @this, Func<TIn, TOut> fn) => fn(@this);
public static async Task<TOut> MapAsync<TIn,TOut>(this TIn @this, Func<TIn, Task<TOut>> fn) => await fn(@this);
public static async Task<TOut> MapAsync<TIn,TOut>(this Task<TIn> @this, Func<TIn, TOut> fn) => fn(await @this);
public static async Task<TOut> MapAsync<TIn,TOut>(this Task<TIn> @this, Func<TIn, Task<TOut>> fn) => await fn(await @this);
// example
@jhoerr
jhoerr / MockDbContext.cs
Last active June 4, 2024 23:56
A mock DbContext for NSubstitute
// Caveats: does not support async enumerations, i.e. ToListAsync, ToArrayAsync
public class MockDbContext<T>
where T : Entity
{
public static ApplicationDbContext Create() => Create(new List<T>());
public static ApplicationDbContext Create(List<T> entities)
{
var queryable = entities.AsQueryable();
from boxsdk import JWTAuth
from boxsdk import Client
# automation id => 32828.... What to do with this?
auth = JWTAuth(
client_id='8grfp2...',
client_secret='1FwTmL...',
enterprise_id='322105',
jwt_key_id='qasd...',
rsa_private_key_file_sys_path='c:\\temp\\private_key_nopass.pem'