ASP.NET MVC 下的 SSL 页面
如何对基于 ASP.NET MVC 的站点中的某些页面使用 HTTPS?
Steve Sanderson 有一个非常好的教程,介绍如何在预览版 4 上以 DRY 方式执行此操作,网址为:
http://blog.codeville.net/2008/08/05/adding-httpsssl-support-to-aspnet-mvc-routing/
有吗预览版 5 有更好/更新的方式吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果您使用ASP.NET MVC 2 Preview 2 或更高版本,您现在可以简单地使用:
不过,顺序参数值得注意,如 这里提到。
If you are using ASP.NET MVC 2 Preview 2 or higher, you can now simply use:
Though, the order parameter is worth noting, as mentioned here.
正如 Amadiere 所写的,[RequireHttps] 在 MVC 2 中效果很好用于输入 HTTPS。 但是,如果您只想对某些页面使用 HTTPS,正如您所说,MVC 2 不会给您任何好处 - 一旦它将用户切换到 HTTPS,他们就会卡在那里,直到您手动重定向他们。
我使用的方法是使用另一个自定义属性 [ExitHttpsIfNotRequired]。 当附加到控制器或操作时,如果满足以下条件,这将重定向到 HTTP:
它有点太大了,无法在这里发布,但您可以看到 此处的代码以及一些其他详细信息。
As Amadiere wrote, [RequireHttps] works great in MVC 2 for entering HTTPS. But if you only want to use HTTPS for some pages as you said, MVC 2 doesn't give you any love - once it switches a user to HTTPS they're stuck there until you manually redirect them.
The approach I used is to use another custom attribute, [ExitHttpsIfNotRequired]. When attached to a controller or action this will redirect to HTTP if:
It's a bit too big to post here, but you can see the code here plus some additional details.
MVCFutures 具有“RequireSSL”属性。
(感谢 Adam 在更新的博文中指出这一点)
只需申请如果您希望 http:// 请求自动变为 https:// ,请使用“Redirect=true”将其添加到您的操作方法中:
另请参阅:仅在生产中使用 ASP.NET MVC RequireHttps
MVCFutures has a 'RequireSSL' attribute.
(thanks Adam for pointing that out in your updated blogpost)
Just apply it to your action method, with 'Redirect=true' if you want an http:// request to automatically become https:// :
See also: ASP.NET MVC RequireHttps in Production Only
这是 Dan Wahlin 最近发表的一篇关于此问题的文章:
http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx
他使用了 ActionFilter属性。
Here's a recent post from Dan Wahlin on this:
http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx
He uses an ActionFilter Attribute.
一些 ActionLink 扩展:http://www.squaredroot。 com/post/2008/06/11/MVC-and-SSL.aspx
或者重定向到 https:// http://forums 的控制器操作属性。 asp.net/p/1260198/2358380.aspx#2358380
Some ActionLink extensions: http://www.squaredroot.com/post/2008/06/11/MVC-and-SSL.aspx
Or an controller action attribute that redirects to https:// http://forums.asp.net/p/1260198/2358380.aspx#2358380
对于那些不喜欢面向属性的开发方法的人,这里有一段代码可以提供帮助:
避免使用属性有几个原因,其中之一是如果您想查看所有受保护页面的列表,您将必须跳过解决方案中的所有控制器。
For those who are not a fan of attribute-oriented development approaches, here is a piece of code that could help:
There are several reasons to avoid attributes and one of them is if you want to look at the list of all secured pages you will have to jump over all controllers in solution.
我遇到了这个问题,希望我的解决方案可以帮助别人。
我们遇到了一些问题:
- 我们需要保护特定操作,例如“帐户”中的“登录”。 我们可以使用 RequireHttps 属性中的构建,这很棒 - 但它会将我们重定向回 https://。
- 我们应该让我们的链接、表格等“SSL 意识”。
一般来说,我的解决方案除了能够指定协议之外,还允许指定使用绝对 url 的路由。 您可以使用此方法来指定“https”协议。
因此,首先我创建了一个 ConnectionProtocol 枚举:
现在,我创建了 RequireSsl 的手动版本。 我修改了原始 RequireSsl 源代码以允许重定向回 http:// url。 此外,我还设置了一个字段,允许我们确定是否需要 SSL(我将其与 DEBUG 预处理器一起使用)。
现在,此 RequireSsl 将根据您的要求属性值执行以下操作:
- 忽略:什么也不做。
- Http:将强制重定向到 http 协议。
- https:将强制重定向到 https 协议。
您应该创建自己的基本控制器并将该属性设置为 Http。
现在,在每个 cpntroller/action 中,您希望需要 SSL - 只需使用 ConnectionProtocol.Https 设置此属性即可。
现在让我们转向 URL:我们在 url 路由引擎方面遇到了一些问题。 您可以在 http 阅读有关它们的更多信息://blog.stevensanderson.com/2008/08/05/adding-httpsssl-support-to-aspnet-mvc-routing/。 这篇文章中建议的解决方案理论上很好,但很旧,而且我不喜欢这种方法。
我的解决方案如下:
创建基本“Route”类的子类:
public class AbsoluteUrlRoute : Route
{
#region ctor
这个版本的“Route”类将创建绝对url。 这里的技巧以及博客文章作者的建议是使用 DataToken 来指定方案(示例在最后:))。
现在,如果我们生成一个 url,例如路由“Account/LogOn”,我们将得到“/http ://example.com/Account/LogOn” - 这是因为 UrlRoutingModule 将所有 url 视为相对的。 我们可以使用自定义 HttpModule 修复此问题:
由于此模块覆盖了 UrlRoutingModule 的基本实现,因此我们应该删除基本 httpModule 并在 web.config 中注册我们的。 因此,在“system.web”下设置:
就是这样:)。
为了注册绝对/遵循协议的路线,您应该这样做:
很乐意听到您的反馈+改进。 希望它能有所帮助! :)
编辑:
我忘记包含 IsCurrentConnectionSecured() 扩展方法(片段太多:P)。 这是一个扩展方法,通常使用Request.IsSecuredConnection。 但是,当使用负载平衡时,此方法将不起作用 - 因此此方法可以绕过此方法(取自 nopCommerce)。
I went accross this question and hope my solution can helps someone.
We got few problems:
- We need to secure specific actions, for instance "LogOn" in "Account". We can use the build in RequireHttps attribute, which is great - but it'll redirect us back with https://.
- We should make our links, forms and such "SSL aware".
Generally, my solution allows to specify routes that will use absolute url, in addition to the ability to specify the protocol. You can use this approch to specify the "https" protocol.
So, firstly I've created an ConnectionProtocol enum:
Now, I've created hand-rolled version of RequireSsl. I've modified the original RequireSsl source code to allow redirection back to http:// urls. In addition, I've put a field that allows us to determine if we should require SSL or not (I'm using it with the DEBUG pre-processor).
Now, this RequireSsl will do the following base on your Requirements attribute value:
- Ignore: Won't do nothing.
- Http: Will force redirection to http protocol.
- Https: Will force redirection to https protocol.
You should create your own base controller and set this attribute to Http.
Now, in each cpntroller/action you'd like to require SSL - just set this attribute with ConnectionProtocol.Https.
Now lets move to URLs: We got few problems with the url routing engine. You can read more about them at http://blog.stevensanderson.com/2008/08/05/adding-httpsssl-support-to-aspnet-mvc-routing/. The solution suggested in this post is theoreticly good, but old and I don't like the approch.
My solutions is the following:
Create a subclass of the basic "Route" class:
public class AbsoluteUrlRoute : Route
{
#region ctor
This version of "Route" class will create absolute url. The trick here, followed by the blog post author suggestion, is to use the DataToken to specify the scheme (example at the end :) ).
Now, if we'll generate an url, for example for the route "Account/LogOn" we'll get "/http://example.com/Account/LogOn" - that's since the UrlRoutingModule sees all the urls as relative. We can fix that using custom HttpModule:
Since this module is overriding the base implementation of UrlRoutingModule, we should remove the base httpModule and register ours in web.config. So, under "system.web" set:
Thats it :).
In order to register an absolute / protocol followed route, you should do:
Will love to hear your feedback + improvements. Hope it can help! :)
Edit:
I forgot to include the IsCurrentConnectionSecured() extension method (too many snippets :P). This is an extension method that generally uses Request.IsSecuredConnection. However, this approch will not work when using load-balancing - so this method can bypass this (took from nopCommerce).
或者将过滤器添加到 Global.asax.cs
RequireHttpsAttribute 类
Alternately add a filter to Global.asax.cs
RequireHttpsAttribute Class
MVC 6 (ASP.NET Core 1.0) 与 Startup.cs 的工作方式略有不同。
要在所有页面上使用 RequireHttpsAttribute(如 Amadiere 的答案中所述),您可以在 Startup.cs 中添加它,而不是使用每个控制器上的属性样式(或者不是为所有控制器创建一个 BaseController 来继承)。
Startup.cs - 注册过滤器:
有关上述方法的设计决策的更多信息,请参阅我对有关 如何排除 RequireHttpsAttribute 处理的 localhost 请求。
MVC 6 (ASP.NET Core 1.0) is working slightly different with Startup.cs.
To use RequireHttpsAttribute (as mentioned in answer by Amadiere) on all pages, you could add this in Startup.cs instead of using attribute style on each controller (or instead of creating a BaseController for all your controllers to inherit from).
Startup.cs - register filter:
For more info about design decisions for above approach, see my answer on similar question about how to exclude localhost requests from being handled by the RequireHttpsAttribute.
这不一定是 MVC 特定的,但此解决方案确实适用于 ASP.NET WebForms 和 MVC:
http://www.codeproject.com/KB/web-security/WebPageSecurity_v2.aspx
我已经使用它好几年了,喜欢通过 web.config 文件分离关注点和管理。
This isn't necessarily MVC specific, but this solution does work for both ASP.NET WebForms and MVC:
http://www.codeproject.com/KB/web-security/WebPageSecurity_v2.aspx
I've used this for several years and like the separation of concerns and management via the web.config file.
这是 Adam Salvo 的博客文章,它使用了 ActionFilter。
Here's a blog post by Adam Salvo that uses an ActionFilter.
这是 Pablo M. Cibrano 于 2009 年 1 月发表的博客文章,其中收集了一些技术,包括 HttpModule 和扩展方法。
Here's a blog post by Pablo M. Cibrano from January 2009 that gathers up a couple of techniques including a HttpModule and extension methods.