Skip to content

Instantly share code, notes, and snippets.

public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(cookieOptions => {
cookieOptions.LoginPath = "/";
@page
@model AuthenticationSample.Pages.Admin.IndexModel
@{
}
<h1>Admin</h1>
using System;
using Microsoft.AspNetCore.Identity;
namespace PasswordHasherTest
{
class Program
{
static void Main(string[] args)
{
var passwordHasher = new PasswordHasher<string>();
Console.WriteLine(passwordHasher.HashPassword(null, "strong password"));
services.Configure<RouteOptions>(options =>
{
options.ConstraintMap.Add("slug", typeof(SlugParameterTransformer));
});
public class SlugParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
return value?.ToString().ToSlug();
}
}
using System.Text;
public static class Extensions
{
public static string ToSlug(this string value, bool toLower = true)
{
if (value == null)
return "";
var normalised = value.Normalize(NormalizationForm.FormKD);
const int maxlen = 80;
int len = normalised.Length;
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.Add(new PageRouteTransformerConvention(new HyphenateRouteParameterTransformer()));
});
using Microsoft.AspNetCore.Routing;
using System.Text.RegularExpressions;
public class HyphenateRouteParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null)
{
return null;
public interface IProductService
{
List<string> GetProductNames();
}
public class ProductService : IProductService
{
public List<string> GetProductNames()
{
return new List<string>
public class ProductConstraint : IRouteConstraint
{
private readonly IProductService productService;
public ProductConstraint(IProductService productService)
{
this.productService = productService;
}
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)