如何在 C# 中将站点配置为自动从 HTTP 重定向到 HTTPS

发布于 2024-12-03 03:46:38 字数 810 浏览 0 评论 0原文

大约一个月前,我为我的网站设置了 SSL。但是,我在网站上还没有习惯这一点。 这些是我的问题:

  • 我手动输入 https URL,因此自动重定向不起作用。
  • SSL 在 https://www.mywebsite.com/a.htm 上一直正常工作,但它不适用于这些链接,例如default.aspx、login.aspx。
  • 如果我在某些页面上手动输入 https,它会自动从 https 重定向到 http。

代码:

protected void Page_Load(object sender, EventArgs e)
{

    Response.Clear();
    Response.Buffer = true;
    //  Response.ContentEncoding = System.Text.Encoding.Default;
    Response.ContentType = "text/html; charset=windows-1254";

    if (Request.Url.Scheme == "https")
    {
        string URL = Request.Url.ToString();
        URL = URL.Replace("https://", "http://");
        Response.Redirect(URL);
    }
}

我的问题是:如何在 C# 中配置站点自动从 HTTP 重定向到 HTTPS?

I set up SSL for my web site about one month ago.However,I have not never been used to that on the web site.
These are my problems:

  • I entering manually as https the URL, so auto redirect not working.
  • SSL have been working no problem at the https://www.mywebsite.com/a.htm ,but it's not working on those links e.g default.aspx , login.aspx.
  • if I entered https as manually on some those pages,it redirecting automatically from https to http.

Code:

protected void Page_Load(object sender, EventArgs e)
{

    Response.Clear();
    Response.Buffer = true;
    //  Response.ContentEncoding = System.Text.Encoding.Default;
    Response.ContentType = "text/html; charset=windows-1254";

    if (Request.Url.Scheme == "https")
    {
        string URL = Request.Url.ToString();
        URL = URL.Replace("https://", "http://");
        Response.Redirect(URL);
    }
}

My question is:how to configure a site to redirect automatically from HTTP to HTTPS in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

子栖 2024-12-10 03:46:38

检查 http 是否重定向到 https

if(!Request.IsSecureConnection) 
{ 
    string redirectUrl = Request.Url.ToString().Replace("http:", "https:"); 
    Response.Redirect(redirectUrl); 
}

Check if http redirects to https:

if(!Request.IsSecureConnection) 
{ 
    string redirectUrl = Request.Url.ToString().Replace("http:", "https:"); 
    Response.Redirect(redirectUrl); 
}
轻许诺言 2024-12-10 03:46:38

Global.asax页面的Application_BeginRequest方法中,检查请求是否为http。如果是这样,请将 http 替换为 https

if (!HttpContext.Current.Request.IsSecureConnection)
{
    string redirectUrl = HttpContext.Current.Request.Url.ToString().Replace("http:", "https:");
    HttpContext.Current.Response.Redirect(redirectUrl);
}

In Application_BeginRequest method of Global.asax page, check if the request is http. If so, replace the http with https:

if (!HttpContext.Current.Request.IsSecureConnection)
{
    string redirectUrl = HttpContext.Current.Request.Url.ToString().Replace("http:", "https:");
    HttpContext.Current.Response.Redirect(redirectUrl);
}
牵你的手,一向走下去 2024-12-10 03:46:38

就我个人而言,我更喜欢 IIS 的 URL 重写模块:
https://www.iis.net/downloads/microsoft/url-rewrite

安装模块后,您可以在 web.config 中添加重定向:

<system.webServer>
    <rewrite>
        <rules>
                <clear />
                <rule name="https" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
                </rule>
        </rules>
    </rewrite>
  </system.webServer>

Personally i prefer the Url-Rewrite Module for IIS:
https://www.iis.net/downloads/microsoft/url-rewrite

After installing the moudle you can add the redirect in web.config:

<system.webServer>
    <rewrite>
        <rules>
                <clear />
                <rule name="https" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
                </rule>
        </rules>
    </rewrite>
  </system.webServer>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文