Skip to content

Instantly share code, notes, and snippets.

@vanillajonathan
vanillajonathan / Main.cs
Last active August 14, 2020 09:57
A very basic general-purpose substitution-based template engine.
public void Main()
{
var template = "Hello {{Name}}, how are you?";
var model = new { Name = "Alice" };
var output = TemplateEngine.Execute(template, model);
// Output: "Hello Alice, how are you?"
}
@vanillajonathan
vanillajonathan / index.html
Created September 22, 2015 17:28
Input validation with Bootstrap warning
<div class="form-group">
<label class="control-label" for="url">URL</label>
<input class="form-control" type="url" id="url" placeholder="https://api.example.com/" />
<p class="help-block" id="url-help"></p>
</div>
@vanillajonathan
vanillajonathan / Index.cshtml
Last active October 31, 2023 12:42
TinyMCE image upload on ASP.NET MVC
<script src="//tinymce.cachefly.net/4.2/tinymce.min.js"></script>
<script>
tinymce.init({
selector: 'textarea',
images_upload_url: "TinyMceUpload",
});
function upload(form) {
tinymce.activeEditor.uploadImages(function (success) {
form.submit();
@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
@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 / 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 / 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 / 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 / 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 / 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
{