Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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.