【发布时间】:2018-08-19 10:38:33
【问题描述】:
我正在使用 ASP.NET 核心 2.1, 我正在寻找一种在一个控制器上强制执行 https 并在另一个控制器上强制执行 http 的方法。
以下文档显示了如何为整个 ASP.NET 核心强制实施 HTTPS,而不是对单个控制器强制实施 HTTPS。
【问题讨论】:
标签: asp.net-core asp.net-core-webapi
我正在使用 ASP.NET 核心 2.1, 我正在寻找一种在一个控制器上强制执行 https 并在另一个控制器上强制执行 http 的方法。
以下文档显示了如何为整个 ASP.NET 核心强制实施 HTTPS,而不是对单个控制器强制实施 HTTPS。
【问题讨论】:
标签: asp.net-core asp.net-core-webapi
一种方法是使用两种操作过滤器:一种用于强制执行 HTTPS 重定向,另一种用于允许 HTTP 请求。第一个将在全球范围内注册,第二个仅用于您希望允许 HTTP 流量的控制器/操作。举个例子:
[AllowHttp]
public class HomeController : Controller
其中AllowHttp定义为:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
// Inheriting from ActionFilterAttribute allows this to show
// up in the ActionExecutingContext.Filters collection.
// See the global filter's implementation.
public class AllowHttpAttribute : ActionFilterAttribute
{
}
接下来,全局过滤器:
// Needed for the GetEncodedUrl() extension method.
using Microsoft.AspNetCore.Http.Extensions;
public class RedirectToHttpsActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.Filters.Any(x => x is AllowHttpAttribute))
{
return;
}
if (!context.HttpContext.Request.IsHttps)
{
var insecureUri = context.HttpContext.Request.GetEncodedUrl();
var secureUri = insecureUri.Replace("http://", "https://");
// As you're likely trying this out locally, you'll need to specify
// the port to redirect to as well. You won't need this on production.
// Change the first port to your HTTP port and the second to your HTTPS port.
secureUri = secureUri.Replace(":49834", ":44329");
context.Result = new RedirectResult(secureUri);
}
}
}
最后,您必须在 Startup.cs 中全局注册过滤器:
services.AddMvc(options =>
{
options.Filters.Add(new RedirectToHttpsActionFilter());
});
我相信您可以通过 URL 重写来实现相同的效果,但是在您可能更改控制器路由的情况下,这将继续工作而无需干预。
【讨论】:
ActionFilterAttribute 位于 Microsoft.AspNetCore.Mvc.Filters 命名空间中。
ConfigureServices方法获取环境,在开发环境中避免注册过滤器。见:stackoverflow.com/a/46551596/729541