mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Squidex.Web.Pipeline
|
|
{
|
|
public sealed class EnforceHttpsMiddleware : IMiddleware
|
|
{
|
|
private readonly UrlsOptions urls;
|
|
|
|
public EnforceHttpsMiddleware(IOptions<UrlsOptions> urls)
|
|
{
|
|
this.urls = urls.Value;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
|
{
|
|
if (!urls.EnforceHTTPS)
|
|
{
|
|
await next(context);
|
|
}
|
|
else
|
|
{
|
|
var hostName = context.Request.Host.ToString().ToLowerInvariant();
|
|
|
|
if (!string.Equals(context.Request.Scheme, "https", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
var newUrl = string.Concat("https://", hostName, context.Request.Path, context.Request.QueryString);
|
|
|
|
context.Response.Redirect(newUrl, true);
|
|
}
|
|
else
|
|
{
|
|
await next(context);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|