在我的旧版ASP.NET MVC 5中,我有一个管理HTTP连接的自定义过滤器。我的过滤器强制通过HTTP重新发送安全(HTTPS)请求
这是我的过滤器:
/// <summary>
/// Forces a secured (HTTPS) request to be resent over HTTP
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class RequireHttpAttribute : FilterAttribute, IAuthorizationFilter
{
public virtual void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.HttpContext.Request.IsLocal)
{
return;
}
if (string.Equals(filterContext.HttpContext.Request.Headers["X-Forwarded-Proto"],
"https",
StringComparison.InvariantCultureIgnoreCase))
{
HandleHttpsRequest(filterContext);
}
return;
}
protected virtual void HandleHttpsRequest(AuthorizationContext filterContext)
{
if (!string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
// Only redirect GET requests
return;
}
string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
filterContext.Result = new RedirectResult(url);
}
}
我如何在ASP.NET 5中执行此操作?
感谢。
答案 0 :(得分:3)
获取内置RequireHttpsAttribute的代码,好吧,让它做相反的事情。
X-Forwarded-For- *的解析是另一个middleware的一部分,所以我不是自己动手,而是先配置它。