1

背景 + 元の問題 (回避策を使用)

一部のページを HTTP として、一部を HTTPS として提供したい Web サイトがあります。これを行うために、次のことを行う関数を作成しました。

        public static void ChangeRequestWithSecurity()
    {
        string absolutePath = HttpContext.Current.Request.Url.AbsolutePath;
        if (HttpContext.Current.Request.ServerVariables["HTTPS"].Equals("on") && !IsSecure(absolutePath))
        {
            ErrorHandling.JustWriteToDatabase("Changing from HTTPS to HTTP: " + HttpContext.Current.Request.Url.AbsoluteUri.Replace("https://", "http://"), "DEBUG");
            //Redirect to http version
            HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("https://", "http://"));
        }
        else if (!HttpContext.Current.Request.ServerVariables["HTTPS"].Equals("on") && IsSecure(absolutePath))
        {
            ErrorHandling.JustWriteToDatabase("Changing from HTTP to HTTPS: " + HttpContext.Current.Request.Url.AbsoluteUri.Replace("http://", "https://"), "DEBUG");
            //Redirect to https version
            HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("http://", "https://"));
        }
        else ErrorHandling.JustWriteToDatabase("Got correct Path: " + HttpContext.Current.Request.Url.AbsoluteUri, "DEBUG");
    }

現在、これを Application_BeginRequest メソッドの下の Global.asax ファイルに配置しています。これにふさわしい場所があれば教えてください!

IsSecure は、リストされている web.config ファイルのセクションからページ名を検索するメソッドであり、それらが安全である必要がある場合は true を返します。

  <SecurePages>
  <add key="/XXXX/Login.aspx" value="page" />
  <add key="/XXXX/Admin.aspx" value="page" />
  <add key="/XXXX/Test.aspx" value="page" />
  <add key="/XXXX/Scripts/ImportantScript.js" value="page" />

これは通常、Web サイトを閲覧する場合は問題なく機能しますが、Response.Redirect("~/TestPage.aspx"); を実行しようとすると、

これは本番環境では壊れ、HTTP または HTTPS 情報なしでページが提供されます。

http://www.mywebsite.com/XXXX/TestPage.aspxの代わりにwww.mywebsite.com/XXXX/TestPage.aspx

なぜこれが起こっているのかわかりません。

この回避策として、Response.Redirect の使用をやめ、カスタム リダイレクタを作成しましたが、これは必要ではないと感じています。

public static void RedirectWithSecurity(string newPage)
    {
        string oldAbsoluteUri = HttpContext.Current.Request.Url.AbsoluteUri;
        string oldUriAddress = HttpContext.Current.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1];
        string newUriStripped = oldAbsoluteUri.Remove(oldAbsoluteUri.LastIndexOf(oldUriAddress));
        string path =newUriStripped+ newPage;
        if (IsSecure(path) && path.Contains("http://")) path = path.Replace("http://", "https://");
        else if (!IsSecure(path) && path.Contains("https://")) path = path.Replace("https://", "http://");

        ErrorHandling.JustWriteToDatabase("CODE REDIRECT: FROM: " + oldAbsoluteUri + " TO: " + path, "DEBUG");
        HttpContext.Current.Response.Redirect(path);
    }

現在の問題、回避策の結果ではないことはほぼ確実

したがって、これは基本的なページとリダイレクトで完全に機能しますが、AjaxControlToolKit コンポーネントが HTTPS を使用するページで機能しなくなりました。fiddler を使用してネットワーク トラフィックを調べると、HTTPS ページの WebResource.axd ファイルが HTTP 経由で送信されていることに気付きました...しかし、誰か教えてもらえますか?

ここで1つの潜在的な解決策を見つけました:http://solutionmania.com/blog/2009/8/1/automatically-switching-to-ssl-in-asp-netしかし、それは別の回避策であり、理解して対処したいと思います根本的な問題で。

使用したプラットフォーム: IIS 7、ASP.NET 4.5。ああ、関連する場合はMasterPagesを使用しています。(ToolkiScriptManager は Masterpage で定義されています)

4

0 に答える 0