带 www 前缀的 ASP.NET HTTP 到 HTTPS 重定向

发布于 2024-09-25 03:05:50 字数 394 浏览 6 评论 0原文

我正在使用这个简单的代码将 http 重定向到我的计费登录页面上的 https:

if (!Request.IsSecureConnection)
{
    // send user to SSL 
    string serverName =HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);        
    string filePath = Request.FilePath;
    Response.Redirect("https://" + serverName + filePath);
}

我还需要它检查 www 并将其添加到 url(如果它不在 url 中)。我需要在这段代码中添加什么来完成这个任务?

I am using this simple code to redirect http to https on my billing landing page:

if (!Request.IsSecureConnection)
{
    // send user to SSL 
    string serverName =HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);        
    string filePath = Request.FilePath;
    Response.Redirect("https://" + serverName + filePath);
}

I need it to also check for and add www to the url if it is not already in the url. What do I need to add to this code to accomplish this?

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

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

发布评论

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

评论(2

伊面 2024-10-02 03:05:51

以下代码假设服务器名称不以“www”开头。那么补救措施就是在当前服务器名前面加上“www”。

if (!Request.IsSecureConnection)
{
    // send user to SSL 
    string serverName = Request.ServerVariables["SERVER_NAME"];
    if (!serverName.ToLowerCaseInvariant().StartsWith("www.")) {
       serverName = string.Format("www.{0}", serverName);
    }
    string filePath = Request.FilePath;
    Response.Redirect("https://" + serverName + filePath);
}

就我个人而言,我不喜欢这种做事方法。我通常创建一个名为 SecureDomain 之类的设置,然后使用逻辑来验证当前的 ServerName 是否与之匹配。像这样的东西。

// Suppose the value of GlobalAppSettings.SecureDomain
// is something like www.securestore.com

if (!Request.IsSecureConnection)
{
    // send user to SSL 
    string serverName = Request.ServerVariables["SERVER_NAME"];
    if (string.Compare(serverName, GlobalAppSettings.SecureDomain, true) != 0) {
       serverName = GlobalAppSettings.SecureDomain;
    }
    string filePath = Request.FilePath;
    Response.Redirect("https://" + serverName + filePath);
}

The following code assumes that if the server name doesn't start with a "www." then the remedy is to prepend whatever the current servername is with "www."

if (!Request.IsSecureConnection)
{
    // send user to SSL 
    string serverName = Request.ServerVariables["SERVER_NAME"];
    if (!serverName.ToLowerCaseInvariant().StartsWith("www.")) {
       serverName = string.Format("www.{0}", serverName);
    }
    string filePath = Request.FilePath;
    Response.Redirect("https://" + serverName + filePath);
}

Personally, I don't like this method of doing things. I usually create a setting named something like SecureDomain and then use logic to verify whether the current ServerName matches that. Something like this.

// Suppose the value of GlobalAppSettings.SecureDomain
// is something like www.securestore.com

if (!Request.IsSecureConnection)
{
    // send user to SSL 
    string serverName = Request.ServerVariables["SERVER_NAME"];
    if (string.Compare(serverName, GlobalAppSettings.SecureDomain, true) != 0) {
       serverName = GlobalAppSettings.SecureDomain;
    }
    string filePath = Request.FilePath;
    Response.Redirect("https://" + serverName + filePath);
}
合约呢 2024-10-02 03:05:50

像这样:

if (!serverName.StartsWith("www."))
    serverName = "www." + serverName;

Like this:

if (!serverName.StartsWith("www."))
    serverName = "www." + serverName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文