Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.Net重定向到安全_C#_Html_Asp.net_Iis_Redirect - Fatal编程技术网

C# ASP.Net重定向到安全

C# ASP.Net重定向到安全,c#,html,asp.net,iis,redirect,C#,Html,Asp.net,Iis,Redirect,我想将页面重定向到ASPX文件的安全连接 客户端被要求复制一个类似于foo.com.au的URL并粘贴到浏览器中 下面的代码正在处理代码隐藏文件,但我想知道它何时部署到生产环境中,是否会在https://www因为提供给客户端的URL中没有www protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); if (!Request.IsLocal && !Reque

我想将页面重定向到ASPX文件的安全连接

客户端被要求复制一个类似于foo.com.au的URL并粘贴到浏览器中

下面的代码正在处理代码隐藏文件,但我想知道它何时部署到生产环境中,是否会在
https://www
因为提供给客户端的URL中没有
www

protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        if (!Request.IsLocal && !Request.IsSecureConnection)
        {
            string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
            Response.Redirect(redirectUrl);
        }
    }

使用,而不是使用Request.Url。此外,您不应假定URL将以小写形式输入。我会将守则修订为:

if (!Request.IsLocal && !Request.IsSecureConnection)
{
    if (Request.Url.Scheme.Equals(Uri.UriSchemeHttp, StringComparison.InvariantCultureIgnoreCase))
    {
        string sNonSchemeUrl = Request.Url.AbsoluteUri.Substring(Uri.UriSchemeHttp.Length);
        // Ensure www. is prepended if it is missing
        if (!sNonSchemeUrl.StartsWith("www", StringComparison.InvariantCultureIgnoreCase)) {
            sNonSchemeUrl = "www." + sNonSchemeUrl;
        }
        string redirectUrl = Uri.UriSchemeHttps + sNonSchemeUrl;
        Response.Redirect(redirectUrl);
    }
}
如果您这样做,它将改变的只是模式。那么,如果绝对值是

http://foo.com.au
将更改为

https://foo.com.au

最后一点注意:当我们这样做的时候,我们从来没有在OnPreInit中尝试过,我们总是在Page_Load中执行这个逻辑。我不确定在页面生命周期的这一部分重定向会有什么后果(如果有的话),但如果遇到问题,可以将其移动到页面加载。

这是我的最后一个实现,用于说明
https://foo
而非
https://www.foo

        if (!Request.IsLocal &&
            !Request.Url.AbsoluteUri.StartsWith("https://www.", StringComparison.OrdinalIgnoreCase))
        {
            string translatedUrl;
            string nonSchemeUrl = Request.Url.AbsoluteUri;
            string stringToReplace = (Request.Url.Scheme == Uri.UriSchemeHttp ? Uri.UriSchemeHttp + "://" : Uri.UriSchemeHttps + "://");
            nonSchemeUrl = nonSchemeUrl.Replace(stringToReplace, string.Empty);
            if (!nonSchemeUrl.StartsWith("www", StringComparison.InvariantCultureIgnoreCase))nonSchemeUrl = "www." + nonSchemeUrl;
            translatedUrl = Uri.UriSchemeHttps + "://" + nonSchemeUrl;
            Response.Redirect(nonSchemeUrl);
        }

谢谢,最后一部分是确保
www
被放置在URL中(如果没有),因为证书是针对
https://www.foo.com.au
而非
https://foo.com.au
@competable\u techI不能100%确定这是否必要,但这应该是一个简单的URI测试,看看它是否以www开头,如果不是,就添加它。我可以修改答案以显示这一点。@ojhawkins:好的,答案已修改为包含添加的www。如果它丢失了。是的,我也不确定,因为证书在生产环境中工作时很难测试,而在其他情况下,您从未使用变量translateUrl