使用 ASP.NET MVC 应用程序重定向远离 HTTPS

发布于 2024-08-27 12:50:59 字数 321 浏览 2 评论 0原文

我正在使用 ASP.NET MVC 2,并且有一个通过 HTTPS 保护的登录页面。为了确保用户始终通过 SSL 访问这些页面,我已将属性 [RequireHttps] 添加到控制器。这完美地完成了工作。

当他们成功登录后,我想将他们重定向回 HTTP 版本。但是,没有 [RequireHttp] 属性,我正在努力思考如何实现这一目标。

增加的(潜在的)复杂性是,网站在生产时托管在域的路由上,但出于开发和测试目的,它位于子目录/虚拟目录/应用程序中。

我是否想得太多了?有没有一个简单的解决方案摆在我面前?还是稍微复杂一点?

I'm using ASP.NET MVC 2 and have a login page that is secured via HTTPS. To ensure that the user always accesses those pages via SSL, I've added the attribute [RequireHttps] to the controller. This does the job perfectly.

When they have successfully logged in, I'd like to redirect them back to HTTP version. However, there isn't a [RequireHttp] attribute and I'm struggling to get my head around how I might achieve this.

The added (potential) complication is that the website when in production is hosted at the route of the domain, but for development and testing purposes it is within a sub directory / virtual directory / application.

Am I over-thinking this and is there an easy solution staring me in the face? Or is it a little more complex?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2024-09-03 12:50:59

经过一番挖掘后,我按照自己的思路进行了开发,因为似乎没有一个好的内置解决方案(如前所述,对于 MVC2 应用程序来说,有一个很好的解决方案,其形式为 [RequireHttps])。受到 çağdaş 的解决方案的启发 这个问题,我适应了以下代码:

public class RequireHttp : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // If the request has arrived via HTTPS...
        if (filterContext.HttpContext.Request.IsSecureConnection)
        {
            filterContext.Result = new RedirectResult(filterContext.HttpContext.Request.Url.ToString().Replace("https:", "http:")); // Go on, bugger off "s"!
            filterContext.Result.ExecuteResult(filterContext);
        }
        base.OnActionExecuting(filterContext);
    }
}

我现在可以将其添加到我的控制器方法中,并且它的行为(看起来)符合预期。如果我从 HTTPS 协议重定向到控制器上的索引操作,它将重定向到 HTTP。它只允许对 Index ActionResult 进行 HTTP 访问。

[RequireHttp]
public ActionResult Index() {
    return View();
}

After a bit of digging, I went along the lines of rolling my own as there didn't appear to be a good built-in solution to this (as mentioned, there is a great one for MVC2 applications in the form of [RequireHttps]). Inspired by çağdaş's solution to this problem and I adapated to come up with the following code:

public class RequireHttp : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // If the request has arrived via HTTPS...
        if (filterContext.HttpContext.Request.IsSecureConnection)
        {
            filterContext.Result = new RedirectResult(filterContext.HttpContext.Request.Url.ToString().Replace("https:", "http:")); // Go on, bugger off "s"!
            filterContext.Result.ExecuteResult(filterContext);
        }
        base.OnActionExecuting(filterContext);
    }
}

I can now add this to my Controller methods and it behaves (seemingly) as expected. If I redirect to the Index action on my controller from a HTTPS protocol, it will redirect to HTTP. It only allows HTTP access to the Index ActionResult.

[RequireHttp]
public ActionResult Index() {
    return View();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文