Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@sebnilsson
sebnilsson / keybase.md
Created May 13, 2019 14:42
keybase.md

Keybase proof

I hereby claim:

  • I am sebnilsson on github.
  • I am sebnilsson (https://keybase.io/sebnilsson) on keybase.
  • I have a public key ASCWYybMp3SXcq0oy9xFjsdWBwizgfPCOEE1sCL7ke-Gmwo

To claim this, I am signing this object:

@sebnilsson
sebnilsson / HtmlEncodeTagHelper.cs
Last active January 18, 2019 15:26
Render everything inside tag as encoded HTML
[HtmlTargetElement("html-encode", TagStructure = TagStructure.NormalOrSelfClosing)]
public class HtmlEncodeTagHelper : TagHelper
{
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var childContent = output.Content.IsModified
? output.Content.GetContent()
: (await output.GetChildContentAsync()).GetContent();
string encodedChildContent = WebUtility.HtmlEncode(childContent ?? string.Empty);
@sebnilsson
sebnilsson / HashAlgorithmExtensions.cs
Created December 30, 2017 20:54
Compute hash in .NET using async await
public static class HashAlgorithmExtensions
{
private const int BufferSize = 4096;
public static async Task<byte[]> ComputeHashAsync(this HashAlgorithm hash, Stream inputStream)
{
if (hash == null) throw new ArgumentNullException(nameof(hash));
if (inputStream == null) throw new ArgumentNullException(nameof(inputStream));
hash.Initialize();
@sebnilsson
sebnilsson / Autofac.AutoMapper.Program.cs
Last active February 5, 2019 13:55
Registering Autofac & AutoMapper Circularly
public static void Main(string[] args)
{
var components = RegisterComponents();
Run(components);
}
private static IContainer RegisterComponents()
{
var builder = new ContainerBuilder();
public static class DateTimeRfc3339Extensions
{
public static string ToRfc3339String(this DateTime dateTime)
{
string rfc3339 = dateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo);
return rfc3339;
}
}
@sebnilsson
sebnilsson / RouteCollectionAreasExtensions.cs
Created December 2, 2015 13:26
Manually map an area-specific route
public static class RouteCollectionAreasExtensions
{
public static Route MapAreaRoute(
this RouteCollection routes,
string areaName,
string routeName,
string url,
object defaults = null,
object constraints = null,
string[] namespaces = null)
@sebnilsson
sebnilsson / UriExtensions.cs
Last active November 16, 2017 08:43
Get relative part of an Uri
public static string ToRelative(this Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
return uri.IsAbsoluteUri ? uri.PathAndQuery : uri.OriginalString;
}
@sebnilsson
sebnilsson / FakeHttpContextFactory.cs
Created October 24, 2014 06:55
Create a fake HttpContext, without using mocking-frameworks
public static HttpContext GetFakeHttpContext(
string url,
HttpRequest request = null,
HttpResponse response = null)
{
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
throw new ArgumentOutOfRangeException("url", "The URL must be a well-formed absolute URI.");
}
@sebnilsson
sebnilsson / SuppressXFrameOptionsHeaderAttribute.cs
Created September 30, 2014 08:40
ASP.NET MVC: X-Frame-Options ALLOWALL
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class SuppressXFrameOptionsHeaderAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Headers.Set("X-Frame-Options", "ALLOWALL");
}
}
@sebnilsson
sebnilsson / ToDynamicObjectExtensions.cs
Created August 25, 2014 09:07
Extension-method to convert any object to dynamic ExpandoObject
public static ExpandoObject ToDynamic(this object obj)
{
var expandoObject = new ExpandoObject();
if (obj == null)
{
return expandoObject;
}
var dictionaryValues = new RouteValueDictionary(obj);
if (!dictionaryValues.Any())