Skip to content

Instantly share code, notes, and snippets.

@vanillajonathan
vanillajonathan / CompareToAttribute.cs
Created December 11, 2018 12:03
Data annotation validation attribute that compares one property with another.
using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace Example
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class CompareToAttribute : ValidationAttribute
@vanillajonathan
vanillajonathan / RequireHttpsApiAttribute.cs
Created October 11, 2018 12:05
Rejects requests not requested over HTTPS.
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Example.AspNetCore.Mvc
{
/// <summary>
/// An authorization filter that confirms requests are received over HTTPS.
/// </summary>
namespace Example
{
public static class SeparatorConstants
{
public const char FileSeparator = (char)0x1c;
public const char GroupSeparator = (char)0x1d;
public const char RecordSeparator = (char)0x1e;
public const char UnitSeparator = (char)0x1f;
}
}
@vanillajonathan
vanillajonathan / Token.cs
Created September 27, 2018 13:35
Generate a JWT token
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using Microsoft.IdentityModel.Tokens;
namespace Example
{
class Token
{
@vanillajonathan
vanillajonathan / HttpContentExtensions.cs
Created April 11, 2018 12:49
Serialize content into a ModelStateDictionary.
public static class HttpContentExtensions
{
/// <summary>
/// Serialize the HTTP content to a <see cref="ModelStateDictionary"/> as an asynchronous operation.
/// </summary>
/// <returns>A <see cref="ModelStateDictionary"/> that contains validation errors.</returns>
public static async Task<ModelStateDictionary> ReadAsModelStateAsync(this HttpContent content)
{
var fields = await content.ReadAsAsync<IDictionary<string, string[]>>();
var modelState = new ModelStateDictionary();
@vanillajonathan
vanillajonathan / RouteAuthorization.cs
Created January 24, 2018 14:26
Authorizes requests if the route parameter name equals a claim.
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace WebApplication
{
/// <summary>
/// Specifies that the class or method that this attribute is applied to requires
/// a route template with a route parameter name which value matches the claim
@vanillajonathan
vanillajonathan / ContentSecurityPolicyAttribute.cs
Created November 22, 2017 15:15
Builds a Content Security Policy.
using System;
using System.Diagnostics.Contracts;
using System.Text;
using Microsoft.AspNetCore.Mvc.Filters;
namespace WebApplication
{
/// <summary>
/// A filter that builds a Content Security Policy.
/// Mitigates the risk of XSS vulnerabilities.
@vanillajonathan
vanillajonathan / DescriptionTagHelper.cs
Created November 16, 2017 14:18
ASP.NET Core MVC TagHelper for description using the Display attribute
using System;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace WebApplication.TagHelpers
{
/// <summary>
/// <see cref="ITagHelper"/> implementation targeting &lt;span&gt; elements with an <c>asp-description-for</c> attribute.
/// </summary>
[HtmlTargetElement("span", Attributes = AttributeName)]
@vanillajonathan
vanillajonathan / Startup.cs
Last active October 10, 2017 09:46
ASP.NET Core middleware that forbids HTTP requests
app.Use(async (context, next) =>
{
if (!context.Request.Scheme.IsHttps) {
context.Response.StatusCode = StatusCodes.Status403Forbidden;
await context.Response.WriteAsync("HTTPS is required.");
return;
}
await next.Invoke();
});
@vanillajonathan
vanillajonathan / NotBeforeAttribute.cs
Created June 26, 2017 12:59
Ensures that the end date is not prior to the start date.
using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace DataAnnotations
{
/// <summary>
/// Provides an attribute that compares two properties of a model and