Eri*_*Cal 50 asp.net https http
因此,在我的代码中,我想检测我的登录页面是否被称为http,并将其重定向到https.
我知道有一些非代码方法来修饰这只猫,但是由于令人沮丧的技术原因,我已经支持在代码中进行操作.
if (!Request.IsSecureConnection)
{
string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
Response.Redirect(redirectUrl);
}
Run Code Online (Sandbox Code Playgroud)
所以我把它放在我的Page_Load(...),确保我的调试器使用真正的IIS,而不是VS2008s IIS,并点击调试.
在调试器中,跳转,点击Response.Redirect(" https://localhost/StudentPortal3G/AccessControl/AdLogin.aspx "),点击f5.
获取"Internet Explorere无法显示网页,网址是HTTP,而不是HTTPS.没有收到信息错误...同样的事情发生在调试器中没有运行.
那我错过了什么?它似乎不是火箭科学,我在许多博客上看到了类似的代码......
我究竟做错了什么?我认为它必须是一个完全明显的新秀错误,但我没有看到它.
tva*_*son 82
我也做了一个!Request.IsLocal以确保我没有调试,但是如果你使用的是一个真正的IIS实例,并且在调试时应用了一个不应该成为问题的证书.
if (!Request.IsLocal && !Request.IsSecureConnection)
{
string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
Response.Redirect(redirectUrl, false);
HttpContext.ApplicationInstance.CompleteRequest();
}
Run Code Online (Sandbox Code Playgroud)
注意:此答案假设Controller中的MVC上下文HttpContext是保存当前上下文的属性.如果您还不幸使用WebForms或以简并方式引用上下文,则需要使用HttpContext.Current.ApplicationInstance.CompleteRequest().
注意:我已根据框架文档将此更新为与推荐的模式一致以终止请求.
在页面处理程序中使用此方法终止对一个页面的请求并为另一个页面启动新请求时,请将endResponse设置为false,然后调用CompleteRequest方法.如果为endResponse参数指定true,则此方法为原始请求调用End方法,该方法在完成时抛出ThreadAbortException异常.此异常对Web应用程序性能有不利影响,这就是建议为endResponse参数传递false的原因.有关更多信息,请参阅End方法.
Ted*_*Ted 24
我通常在我的所有页面继承的基类中从OnPreInit调用以下内容.当然,你可以在每一页都这样做......但你现在不想这样做吗?
请注意,我为每个页面都有两个属性,以便我可以为每个页面指定SSL要求(RequiresSSL),同时我也可以覆盖和重定向检查是否需要(使用IgnoreRequiresSSL,这对于您错误页面这样的页面很有帮助重写并且不知道它们是否会被加密,但当然,您可以删除这些以进行简单的设置.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (!IsPostBack)
RedirectAccordingToRequiresSSL();
...
}
/// <summary>
/// Redirect if necessary to ssl or non-ssl enabled URL dependant on RequiresSSL property setting.
/// </summary>
private void RedirectAccordingToRequiresSSL()
{
if (IgnoreRequiresSSL) return;
if (RequiresSSL)
{
if (!Request.IsSecureConnection) // Need to redirect to https
RedirectAccordingToRequiresSSL(Uri.UriSchemeHttps);
}
else if (Request.IsSecureConnection)
{
RedirectAccordingToRequiresSSL(Uri.UriSchemeHttp);
}
// Otherwise don't need to do any redirecting as already using the correct scheme
}
/// <summary>
/// Redirect as requested to specified scheme
/// </summary>
/// <param name="scheme"></param>
private void RedirectAccordingToRequiresSSL(string scheme)
{
var url = scheme + Uri.SchemeDelimiter + Request.Url.Authority + Request.Url.PathAndQuery;
Response.Redirect(url, false);
}
Run Code Online (Sandbox Code Playgroud)
在我看来,以下是最好的全方位方法.
三个原因
MVC,Web API也适用于IIS水平.https在你的电脑上调试非网站时,永久重定向可能会让你烦恼).https只需将以下内容添加到<system.webServer>项目"Web.config"中的部分即可.
<system.webServer>
....
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<outboundRules>
<rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<action type="Rewrite" value="max-age=31536000" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
您还可以使用新的UriBuilder:
Dim context As HttpContext = HttpContext.Current
If Not context.Request.IsSecureConnection Then
Dim secureUrl As New UriBuilder(context.Request.Url)
secureUrl.Scheme = "https"
secureUrl.Port = 443
context.Response.Redirect(secureUrl.ToString, False)
Return
End If
Run Code Online (Sandbox Code Playgroud)
C#
HttpContext context = HttpContext.Current;
if (!context.Request.IsSecureConnection)
{
UriBuilder secureUrl = new UriBuilder(context.Request.Url);
secureUrl.Scheme = "https";
secureUrl.Port = 443;
context.Response.Redirect(secureUrl.ToString(), false);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79640 次 |
| 最近记录: |