我有一个asp.net站点。它有一个订购表,可以在https://secure.example.com/order.aspx上访问。网站上的链接不包括域名。例如,主页是“default.aspx”。
问题是,如果我从安全页面点击一个像主页这样的链接,url将变成https://secure.example.com/default.aspx而不是http://www.example.com/default.aspx。
处理这个问题的好方法是什么?该方案应该自动工作使用任何域名的基础上,它是从哪里启动。因此,如果站点是从'localhost‘启动的,远离受保护的页面,url应该是http://localhost/…
导航链接位于母版页中。
发布于 2010-04-26 02:54:48
我想最好的解决方案是http模块。
下面是它的最简单的实现。useUnsecureConnection变量包含指示是否需要搬走的值(应由您自己计算)。
public class SecurityModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
}
#endregion
#region Events Handling
protected void application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = ((HttpApplication)(sender));
HttpRequest request = application.Request;
HttpResponse response = application.Response;
// here should be you condition to determine
// whether to move away from secure page or not
bool useUnsecureConnection = true;
if (useUnsecureConnection && request.IsSecureConnection)
{
string absoluteUri = request.Url.AbsoluteUri;
response.Redirect(absoluteUri.Replace("https://", "http://"), true);
}
}
#endregion
}当然,别忘了在你的web.config中注册模块:
<httpModules>
<!--Used to redirect secure connections to the unsecure ones
if necessary-->
<add name="Security"
type="{YourNamespace}.Handlers.SecurityModule,
{YourAssembly}" />
...
</httpModules>
</system.web>顺便说一句,对于localhost,条件可能如下所示:
useUnsecureConnection = request.IsLocal;如果请求发起者的IP地址是127.0.0.1,或者如果请求的IP地址与服务器的IP地址相同,则其将为true。
https://stackoverflow.com/questions/2709250
复制相似问题