读书人

一百分怎么使在浏览器输入http://xx

发布时间: 2012-02-15 12:09:44 作者: rapoo

一百分,如何使在浏览器输入http://xxx会自动跳到https://
我在做一个网站,要使用SSL,我发现像PaiPai.com等网站,任意页面,如果使用http访问,都会自动跳到https,这是怎么实现的?
用客户端JS或者服务器Response.Redirect肯定不行。因为我所有页面都要求使用SSL访问。用Http是访问不了的。
我觉得应该是要接管Http Module来实现,但具体怎么做就不清楚了,麻烦哪位大侠说说。

[解决办法]
不懂。帮顶
[解决办法]
Response.Redirect( string.Format( "https://{0}/{1}/{2} ", Request.Url.Host, Request.ApplicationPath,Request.RawUrl));
这样吧
[解决办法]

<P> First, we define a custom configuration class so we can place our URL base
strings into web.config: </P>
<DIV class=precollapse id=premain0 style= "WIDTH: 100% "> <IMG id=preimg0
style= "CURSOR: hand " height=9 src= "http://www.codeproject.com/images/minus.gif "
width=9 preid= "0 "> Collapse </DIV> <PRE lang=cs id=pre0 style= "MARGIN-TOP: 0px "> using System;
using System.Configuration;
namespace AA.switchprotocol
{
public class SwitchProtocolSection : ConfigurationSection
{
[ConfigurationProperty( "urls ", IsRequired = true)]
public UrlsFormElement Urls
{
get { return (UrlsFormElement)base[ "urls "]; }
}
}
public class UrlsFormElement : ConfigurationElement
{
[ConfigurationProperty( "baseUrl ", IsRequired = true)]
public string BaseUrl
{
get { return (string)base[ "baseUrl "]; }
}
[ConfigurationProperty( "baseSecureUrl ", IsRequired = true)]
public string BaseSecureUrl
{
get { return (string)base[ "baseSecureUrl "]; }
}
}
} </PRE>
[解决办法]
Now we can define our base URL strings in web.config:

<?xml version= "1.0 "?>
<configuration>
<configSections>
<section
name= "SwitchProtocol "
type= "AA.switchprotocol.SwitchProtocolSection, __code "/>
</configSections>
<SwitchProtocol>
<urls baseUrl= "http://localhost "
baseSecureUrl= "https://localhost " />
</SwitchProtocol>
<system.web>
<compilation debug= "false "/>
</system.web>


[解决办法]

<P> Next, define a simple accessor to the confiuration settings: </P> <PRE lang=cs> using System;
using System.Web.Configuration;
namespace AA.switchprotocol
{
public static class Globals
{
public readonly static SwitchProtocolSection Settings =
(SwitchProtocolSection)WebConfigurationManager.
GetSection( "SwitchProtocol ");
}
} </PRE>
<P> Implement a base page class with custom redirect logic: </P>
<DIV class=precollapse id=premain3 style= "WIDTH: 100% "> <IMG id=preimg3
style= "CURSOR: hand " height=9 src= "http://www.codeproject.com/images/minus.gif "
width=9 preid= "3 "> Collapse </DIV> <PRE lang=cs id=pre3 style= "MARGIN-TOP: 0px "> using System;
namespace AA.switchprotocol.UI


{
public class BasePage : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
string scheme = Request.Url.Scheme;
if (_issecure)
{
if (scheme != "https ")
{
Response.Redirect(
Globals.Settings.Urls.BaseSecureUrl +
Request.RawUrl);
}
}
else
{
if (scheme != "http ")
{
string to = Globals.Settings.Urls.BaseUrl +
Server.UrlEncode(Request.RawUrl);
Server.Transfer( "~/Tranz.aspx?to= " + to);
}
}
base.OnLoad(e);
}
private bool _issecure = false;
protected bool IsSecure {
get { return _issecure; }
set{ _issecure = value; }
}
}
} </PRE>
<P> Implement Tranz.aspx, which takes care of the script-block redirect: </P> <PRE lang=aspnet> <%@ Page Language= "C# " Theme= " " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN "
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
protected void Page_Load(object sender, EventArgs e)
{
_to = Server.UrlDecode(Request.QueryString[ "to "]);
}
private string _to;
protected void js()
{
Response.Write( "window.location.replace(\ " " + _to + "\ "); ");
}
</script>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head>
<script type= "text/javascript " language= "JavaScript ">
<!--
<% js(); %>
-->
</script>
<title> </title> </head> <body> </body>
</html> </PRE>
<P> Implement a base page which defaults to secure: </P> <PRE lang=cs> using System;
namespace AA.switchprotocol.UI
{
public class SecurePage : BasePage
{
protected override void OnLoad(EventArgs e)
{
IsSecure = true;
base.OnLoad(e);
}
}
} </PRE>
[解决办法]
复杂 MARK
[解决办法]
mark 帮顶
[解决办法]
心情8错 帮顶个
[解决办法]
mark
[解决办法]
up
[解决办法]
应该在web.config文件里做个影射就行
[解决办法]
a new one,,, http://www.codeproject.com/aspnet/WebPageSecurity.asp


sorry, i can not find the former one URL

hope it helps

[解决办法]
收藏
[解决办法]
这个 应该 可以 直接 在httpmodule中对地址 进行处理.
[解决办法]
web.config里可以配置
但是如果要在iis下用https,你需要申请一个数字证书

[解决办法]
用url重写,或者直接在写个page的基类,在基类中自己判断输入的是http就自动跳转到https。
[解决办法]
IIS里设主机头跳转,以前用这个跳转到8080端口
[解决办法]
标记,学习。
[解决办法]
学习!
[解决办法]
vb_vs(我是一只紧张的小星星^_^) 的方法不错收藏了
[解决办法]
URL重写!
[解决办法]
up
[解决办法]
mark
[解决办法]
楼主说的是不是URL重写?
[解决办法]
学习一下
[解决办法]
mark

[解决办法]
mark
[解决办法]
好象验证模式不一样?
[解决办法]
顶上
[解决办法]
看见代码紧张!
[解决办法]
应该用URL重写吧
[解决办法]
帮顶 Mark
[解决办法]
Http Module 可以做到

可以直接判断该Request是否用SSl的
Request.IsSecureConnection = true;

否则的话 自己改请求


[解决办法]
class UrlRedirect : IHttpModule
{
#region IHttpModule Members

public void Dispose()
{
throw new Exception( "The method or operation is not implemented. ");
}

public void Init(HttpApplication context)
{

if (context.Request.IsAuthenticated == false)
{
context.Response.Redirect( "https://xxxxx ",true);
}
}

#endregion
}

如果某些页面不需要 https 挑砖 请在
加入 不需要挑砖的URL判断 用xml 文件隐射下就好
if (context.Request.IsAuthenticated == false)
[解决办法]
我是来学习的。
[解决办法]
看看
[解决办法]
MARK
[解决办法]
学习
[解决办法]
学习。学习。。

读书人网 >asp.net

热点推荐