如何在 ASP.NET MVC 中执行 [RequireHttps(Redirect=true)] 的相反操作

发布于 2024-07-27 02:37:10 字数 383 浏览 3 评论 0原文

我知道在 ASP.NET MVC 中访问 SSL 页面的简单方法 - 通过 [RequireSSL] 属性,但我对执行相反操作的最佳方法有点困惑。

我的网站上的标题栏中有许多链接,其中大多数链接不需要 SSL,而且我不想仍然使用 SSL。

futures 项目可以很容易地使用 [RequireSSL(Redirect=true)] 自动重定向到 SSL 页面,但似乎并不容易脱离此上下文并自动重定向回http。

我缺少什么?

I know the easy way to get to an SSL page in ASP.NET MVC - via the [RequireSSL] attribute but I'm a little confused to the best way to do the opposite.

I have many links on my site in a header bar and most of those links don't require SSL and I don't want to still use SSL.

The futures project makes it very easy to redirect automatically to an SSL page with [RequireSSL(Redirect=true)], but it doesnt seem to make it easy to get out of this context and automatically redirect back to http.

What am I missing?

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

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

发布评论

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

评论(3

甜警司 2024-08-03 02:37:10

你没有遗漏任何东西; 对此没有开箱即用的功能。 您可以通过RequireSslAttribute 源并修改它来轻松创建自己的属性。

You're not missing anything; there is no out-of-the-box functionality for this. You can easily create your own by taking the RequireSslAttribute source and modifying it.

软甜啾 2024-08-03 02:37:10

来自其他地方的欺骗问题的回答:

如何在 asp.net mvc 中从 https 退出到 http 模式。

注意:如果选择使用此方法,您的身份验证 cookie 将在切换回 HTTP 后通过纯文本发送,并且可能被其他人窃取和使用。 查看此内容。 换句话说 - 如果您将其用于银行网站,您需要确保切换到 http 会首先使用户注销。

public class DoesNotRequireSSL: ActionFilterAttribute 
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext) 
        {
                var request = filterContext.HttpContext.Request;
                var response = filterContext.HttpContext.Response;

                if (request.IsSecureConnection && !request.IsLocal) 
                {
                string redirectUrl = request.Url.ToString().Replace("https:", "http:");
                response.Redirect(redirectUrl);
                }
                base.OnActionExecuting(filterContext);
        }
    }

Answer from a dupe question elsewhere:

How to step out from https to http mode in asp.net mvc.

CAUTION: If choosing to use this approach your auth cookie will be sent over plain text after switching back to HTTP, and can potentially be stolen and used by someone else. See this. In other words - if you were using this for a bank site you would need to make sure that switching to http would first log the user out.

public class DoesNotRequireSSL: ActionFilterAttribute 
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext) 
        {
                var request = filterContext.HttpContext.Request;
                var response = filterContext.HttpContext.Response;

                if (request.IsSecureConnection && !request.IsLocal) 
                {
                string redirectUrl = request.Url.ToString().Replace("https:", "http:");
                response.Redirect(redirectUrl);
                }
                base.OnActionExecuting(filterContext);
        }
    }
も让我眼熟你 2024-08-03 02:37:10

这是非常值得一读的(特别是要认识到不小心从 https 切换回 http 的安全隐患:

使用 ASP.NET 的部分 SSL 安全 Web 应用 - 不是 MVC 特定的,但相关的安全问题

部分 SSL 网站ASP.NET MVC - MVC 友好

总的来说,这是一个相当复杂的问题,仍然没有找到我想做的所有事情的真正解决方案,但认为这些文章可能对其他人有帮助。

This is well worth reading (epecially to realize the security implications of switching carelessly back to http from https :

Partially SSL Secured Web Apps With ASP.NET - not MVC specific but relevant security concerns

Partial SSL Website with ASP.NET MVC - MVC friendly

It's quite a complicated issue overall. Still haven't found a true solution to everything I want to do, but thought these articles may help others.

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