本文介绍了重定向在ASP.NET MVC SSL动作过滤器循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ActionFilter(见下文)来检测电流控制器/动作与否1.需要SSL,目前正在使用的2 SSL,并相应地重定向。

这本地工作正常(使用IIS 7的虚拟CERT),但一旦我得到它在服务器上,我得到指示无限重定向循环错误。

任何想法?

 公共类SslFilter:ActionFilterAttribute
{
    公共SslFilter(布尔sslRequired)
    {
        SslRequired = sslRequired;
    }    公共BOOL SslRequired {搞定;组; }
    公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
    {
        HTT prequestBase REQ = filterContext.HttpContext.Request;
        HTT presponseBase解析度= filterContext.HttpContext.Response;        VAR建设者=新UriBuilder(req.Url);        如果(SslRequired&安培;&安培;!req.IsSecureConnection)
        {
            builder.Scheme = Uri.UriSchemeHttps;
            builder.Port = 443;
            res.Redirect(builder.Uri.ToString());        }
        否则,如果(SslRequired&安培;!&安培; req.IsSecureConnection)
        {
            builder.Scheme = Uri.UriSchemeHttp;
            builder.Port = 80;
            res.Redirect(builder.Uri.ToString());
        }        base.OnActionExecuting(filterContext);
    }
}

Firefox的错误:

解决方案

Try redirecting in a more MVCish way:

var builder = new UriBuilder(req.Url);

if (SslRequired && !req.IsSecureConnection)
{
    builder.Scheme = Uri.UriSchemeHttps;
    builder.Port = 443;
    filterContext.Result = new RedirectResult(builder.Uri.ToString());
    filterContext.Cancel = true;
}
else if (!SslRequired && req.IsSecureConnection)
{
    builder.Scheme = Uri.UriSchemeHttp;
    builder.Port = 80;
    filterContext.Result = new RedirectResult(builder.Uri.ToString());
    filterContext.Cancel = true;
}
else
{
    base.OnActionExecuting(filterContext);
}

这篇关于重定向在ASP.NET MVC SSL动作过滤器循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:03