[英]HTTPS for only one ASP.NET page (Login.aspx), HTTP always for rest of site
我有一個帶有SSL證書的ASP.NET 4.0 webforms站點。 我現在可以通過HTTP或HTTPS訪問該網站,它工作正常。 無論該決定的優點還是優點,我的經理想要的是能夠訪問http://example.com/Login.aspx並將其重定向到https://example.com/Login.aspx,以便HTTPS是僅在此Login.aspx頁面上強制執行。 但是,登錄后(Login.aspx將用戶重定向到Default.aspx),站點的其余部分需要始終是常規HTTP。
結論:Login.aspx應始終為HTTPS,無論用戶在訪問網站時是輸入HTTP還是HTTPS。 網站的其余部分應始終為HTTP。 如何通過IIS或代碼解決方案實現此目的?
更新1:這是我工作的編碼解決方案。 我想嘗試使用IIS Rewrite Module,只需等待IT支持人員為我安裝它。 在Application_BeginRequest方法中的Global.asax.cs中調用RequireHttpsOnLogin():
public void RequireHttpsOnLogin()
{
if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false) && HttpContext.Current.Request.FilePath.EndsWith("Login.aspx"))
{
//On HTTP login page on server, redirect to HTTPS
Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
}
else if (HttpContext.Current.Request.IsSecureConnection.Equals(true) && HttpContext.Current.Request.IsLocal.Equals(false) && !HttpContext.Current.Request.FilePath.EndsWith("Login.aspx"))
{
//Not on HTTP login page and on server, redirect to HTTP
Response.Redirect("http://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
}
}
更新2:以下內容適用於登錄頁面為HTTPS,但其他頁面始終不是HTTP。
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(Login.aspx)" ignoreCase="true"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found"/>
</rule>
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{R:1}" pattern="(login.aspx)" negate="true" ignoreCase="true" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
嘗試在Web.config中添加以下內容
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(Login.aspx)"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found"/>
</rule>
</rules>
</rewrite>
</system.webServer>
您可以將代碼放在方法Application_BeginRequest中的Global.asax.cs中,以根據需要強制重定向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.