iis7,https,asp.net mvc的相关问题
IIS7配置SSL/https
view SSL binding configuration stored in HTTP.sys:
netsh http show sslcert
在某一站点上启用SSL:
C:\Windows\system32\inetsrv>appcmd set config "Default Web Site" -commitPath:APPHOST -section:access -sslFlags:[Ssl | SslNegotiateCert | SslRequireCert | Ssl128 | None]
-sslFlags的值要注意大小写
然后iis服务器里设置证书,再给站点做https的bindings,SSL Certificate选刚才建立的证书。
asp.net mvc 使用 SSL / https
首先注意到是,https在没有通过验证时,http的status code要返回:
Disable the Require secure channel option, or use HTTPS instead of HTTP to access the page. If you receive this error for a Web site that does not have a certificate installed, click the article number below to view the article in the Microsoft Knowledge Base:
我们可以创建Filter来完成验证工作:
RequireSSLBaseAttribute
配合做一个SSL未验证的ActionResult:
Code
如果想在访问http://url/的时候跳转到https://url/可以这样继承一个:
1protected override ActionResult BuildActionResult(HttpContextBase httpContext)
2{
3 //TODO: uri
4 //获取当前请求的url并将开头的http转换为https://
5 var url = httpContext.Request.Url.AbsoluteUri.ToString();
6 if(httpContext.Request.Url.Scheme.Equals("http", StringComparison.InvariantCultureIgnoreCase))
7 {
8 var builder = new UriBuilder(httpContext.Request.Url.AbsoluteUri);
9 //将Scheme换成https
10 builder.Scheme = "https";
11 //这里需要将Port设置为-1,因为UriBuilder并没有创建新的uri,所以
12 //如果从http过来的url在这里Port会保留80. 设置为-1时,ToString()
13 //方法内会自动去掉":Port"的项
14 builder.Port = -1;
15 url = builder.ToString();
16 }
17 return new RedirectResult(url);
18}
2{
3 //TODO: uri
4 //获取当前请求的url并将开头的http转换为https://
5 var url = httpContext.Request.Url.AbsoluteUri.ToString();
6 if(httpContext.Request.Url.Scheme.Equals("http", StringComparison.InvariantCultureIgnoreCase))
7 {
8 var builder = new UriBuilder(httpContext.Request.Url.AbsoluteUri);
9 //将Scheme换成https
10 builder.Scheme = "https";
11 //这里需要将Port设置为-1,因为UriBuilder并没有创建新的uri,所以
12 //如果从http过来的url在这里Port会保留80. 设置为-1时,ToString()
13 //方法内会自动去掉":Port"的项
14 builder.Port = -1;
15 url = builder.ToString();
16 }
17 return new RedirectResult(url);
18}