2011-02-09 7 views
37

HTTPサイトをHTTPSにリダイレクトする必要があります。以下のルールを追加しましたが、http://www.example.comを試してみると403エラーが表示されます。https://www.example.comブラウザ。MVCアプリケーション(IIS7.5)でHTTPSをHTTPSにリダイレクトする方法

Global.asax.cs

protected void Application_BeginRequest(){ 
    if (!Context.Request.IsSecureConnection) 
     Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:")); 
} 

をしたり、アクションフィルタに同じコードを追加することができます::

<system.webServer> 
    <rewrite> 
     <rules> 
      <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
       <match url="(.*)" /> 
       <conditions> 
        <add input="{HTTPS}" pattern="off" ignoreCase="true" /> 
       </conditions> 
       <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" /> 
      </rule> 
     </rules> 
    </rewrite> 
</system.webServer> 
+0

を?どういう意味ですか? –

+0

IISの下にある "URL rewirte"モジュールを使用してルールを追加しました。これはweb.configにあります。 – LLM

答えて

99

あなたはコードでそれを行うことができます

public class SSLFilter : ActionFilterAttribute { 

    public override void OnActionExecuting(ActionExecutingContext filterContext){ 
     if (!filterContext.HttpContext.Request.IsSecureConnection){ 
      var url = filterContext.HttpContext.Request.Url.ToString().Replace("http:", "https:"); 
      filterContext.Result = new RedirectResult(url); 
     } 
    } 
} 
+0

私は同じ403の同じ禁じられたエラーを試みました。 – LLM

+0

私のリダイレクトロジックをApplication_BeginRequest()に移動すると、入力したURLに基​​づいてリダイレクトしようとしていたところで問題が解決されました。 – farina

+0

Application_BeginRequest()は私にとって完璧に働いてくれました。ありがとうございます。 –

3

I Global.asaxで次を使用してください:

protected void Application_BeginRequest() 
{ 
    if (FormsAuthentication.RequireSSL && !Request.IsSecureConnection) 
    { 
    Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://")); 
    } 
} 
1

ローカルデバッグセッションは、カスタムポート番号を使用していますので、私は、thuslyそれをやった:

protected void Application_BeginRequest() 
    { 
     if (!Context.Request.IsSecureConnection) 
     { 
      if (HttpContext.Current.Request.IsLocal) 
      { 
       Response.Redirect(Context.Request.Url.ToString().Replace("http://localhost:25885/", "https://localhost:44300/")); 
      } 
      else 
      { 
       Response.Redirect(Context.Request.Url.ToString().Replace("http://", "https://")); 
      } 
     } 
    } 

好ましくは、プログラムURLとSSL URLを取得するいくつかの方法があるだろう...

2

をあなたが使用することができます単純なケースのためのRequireHttpsAttribute MSDNに記載されている...

として

[RequireHttps] 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 

" HTTPSを介して再送信する無担保HTTPリクエストを強制的に属性を表します。"

RequireHttpsAttribute

私はあなたがが大規模なサイト間でHTTPSを強制するために、これを使用したいと思いますか分かりません。飾ることがたくさんあり、コントローラーを見逃す機会があります。

+1

あなたがchildactions(レンダリング);-)を持っている場合は動作しません – juFo

+0

良いスポット、それはどうしますか? – Nattrass

+0

ちょうどクラッシュします。 "子アクションはリダイレクトアクションを実行できません。" "例外の詳細:System.InvalidOperationException:子アクションはリダイレクトアクションを実行できません。 – juFo

3

This投稿は、すべてのリクエストをHTTPSにリダイレクトする方法を非常にうまく説明しています。 Global.asax.cs

15

シンプルリダイレクト

protected void Application_BeginRequest() { if (!Context.Request.IsSecureConnection && !Context.Request.Url.ToString().StartsWith("http://localhost:") // to avoid switching to https when local testing ) { // Only insert an "s" to the "http:", and avoid replacing wrongly http: in the url parameters Response.Redirect(Context.Request.Url.ToString().Insert(4, "s")); } } 

はリダイレクト
301 Moved PermanentlyリダイレクトがにHTTPからユーザーをアップグレードするためのベストプラクティスと考えている

(SEOのベストプラクティス) HTTPS(see Google recommendations)。

だから、GoogleやBingのロボットがあまりにもリダイレクトされます場合は、この考慮してください。ルールの下

protected void Application_BeginRequest() 
{ 
    if (!Context.Request.IsSecureConnection 
     && !Context.Request.Url.ToString().StartsWith("http://localhost:") // to avoid switching to https when local testing 
     ) 
    { 
     Response.Clear(); 
     Response.Status = "301 Moved Permanently"; 
     Response.AddHeader("Location", Context.Request.Url.ToString().Insert(4, "s")); 
     Response.End(); 
    } 
} 
+2

私は5年後に戻ってくるOPを期待していないIMOに答えて選択する必要があります。 – Sinjai

+0

@Matthieu Charbonnierありがとうございます –

+2

私は上記の投稿に同意します - この1つは回答を選択する必要があります –

関連する問題