ASP.NET MVC-RequireHttps-指定重定向URL
我在MVC2应用程序中使用了ASP.NET MVC-RequireHttps-指定重定向URL,asp.net,asp.net-mvc,ssl,https,Asp.net,Asp.net Mvc,Ssl,Https,我在MVC2应用程序中使用了[RequireHttps],但在我的测试机中,SSL url与实际站点url不同(它是一个共享SSL环境) 例如: 我的站点url为,SSL url为 当控制器/操作需要HTTPS(最好在Web.config文件中)时,是否有方法告诉MVC重定向到该url 谢谢。没有办法使用内置的requireHttpAttribute类,但是编写自己的MVC过滤器属性非常简单。类似的东西(基于RequireHttpAttribute类)应该可以工作: public class R
[RequireHttps]
,但在我的测试机中,SSL url与实际站点url不同(它是一个共享SSL环境)
例如:
我的站点url为,SSL url为
当控制器/操作需要HTTPS(最好在Web.config文件中)时,是否有方法告诉MVC重定向到该url
谢谢。没有办法使用内置的
requireHttpAttribute
类,但是编写自己的MVC过滤器属性非常简单。类似的东西(基于RequireHttpAttribute类)应该可以工作:
public class RedirectToHttpsAttribute : FilterAttribute, IAuthorizationFilter
{
protected string Host
{
get;
set;
}
public RedirectToHttpsAttribute ( string host )
{
this.Host = host;
}
public virtual void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}
if (!filterContext.HttpContext.Request.IsSecureConnection) {
HandleHttpsRedirect(filterContext);
}
}
protected virtual void HandleHttpsRedirect(AuthorizationContext context)
{
if ( context.HttpContext.Request.HttpMethod != "GET" )
{
throw new InvalidOperationException("Can only redirect GET");
}
string url = "https://" + this.Host + context.HttpContext.Request.RawUrl;
context.Result = new RedirectResult(url);
}
}
编辑:
我不确定您是否可以在FilterAttribute中读取web.config,但我想不出不读取的原因。很酷的解决方案,但我认为这不值得,因为它仅用于测试服务器(在生产中我们有)。不过还是要谢谢你!这种重定向通常在负载平衡器级别处理;你确定你的应用程序中需要它吗?