2013-12-17 1 views
8

Owinで新しいAsp.Net Identityを使用して、新しいMVC 5サイトを開始しました。属性[Authorize]を持つ私の "account"コントローラでは、私はかなり標準的なアクションを持っています。return_rlへのリダイレクトはAsp.Net MVC5でどのように動作するのですか

// GET: /User/Login 
     [AllowAnonymous] 
     public ActionResult Login(string returnUrl) 
     { 
      ViewBag.ReturnUrl = returnUrl; 
      return View(); 
     } 

// POST: /User/Login 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
     { 
      try 
      { 
       if (ModelState.IsValid) 
       { 
        var userApi = new UserService(); 
        var apiUser = await userApi.LogIn(UserManager, model.CardNumber, model.Pin, model.RememberMe); 

        if (apiUser != null) 
        { 
         await SignInAsync(apiUser, model.RememberMe); 
         if (string.IsNullOrEmpty(returnUrl)) 
         {         
          return RedirectToAction("UserLoggedIn", "User");  
         } 
        } 
        else 
        { 
         ModelState.AddModelError("", "Invalid username or password."); 
        } 
       } 

      } 
      catch (Exception ex) 
      { 
       Trace.TraceError("Cannot login {0}", ex.ToString()); 
       Response.AppendToLog(ex.ToString()); 
       ModelState.AddModelError("", ex.ToString()); 
      } 
      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

私の質問は、にreturnurl行動に関してであり、上記のコードは、ユーザーがログインしていない場合、という意味で動作し、属性[承認]を持っているコントローラでアクションを呼び出して、それが取得します上記のログインアクションに送信され、次に要求されたコントローラ/アクションに戻されます。どちらが素晴らしいですが、どのように?それは安全ですか?

この記事では、 "Preventing open redirect attacks"(以前のバージョンのAsp.Net MVC)については、リダイレクトを行う前にローカルURLであることをreturnUrlで確認することをお勧めします。それは今フレームワークによって処理されましたか?

乾杯、 オラ

答えて

13

あなたは、URLが実際に(それが自動的にフレームワークによって処理されていない)、このメソッドを使用して、ローカルであるかどうかを確認する必要があります:http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.islocalurl%28v=vs.118%29.aspx

if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) 
{ 
    return Redirect(returnUrl); 
} 
+0

あなたはそれが処理されないことを見るための任意のソースを持っていますか? returnUrlに自動的にリダイレクトする機能を追加するのではなく、独自のガイドラインに従わないというのは奇妙なことです。したがって元の質問。 –

+0

returnUrlを操作して何が起こるのを見てみませんか?ところで、あなたのコードでは、 'returnUrl'引数は決して使用されません。 – Marthijn

+0

ええ、それは1つのオプションになると思います。私はreturnUrlが決して私のアクションで使われていないことを知っていますが、それでもサイトは正しくリダイレ​​クトされています。それがどういう仕組みか、魔法か、まったく何かが並んでいるのだろうか?そのもの? –

3
 if (Url.IsLocalUrl(returnUrl)) 
     { 
      return Redirect(returnUrl); 
     } 
     else 
     { 
      return RedirectToAction("Index", "Controller"); 
     } 
+0

私はこのコードを知っていますが、もし私がそれを使う必要があるのか​​、それともMVC5のフレームワークで扱われるのかという疑問があります。 –

2

答えるために、あなたのリダイレクトURLがどのように設定されているかについての最初の質問はStartup.Auth.csで設定されています。これはStartup.csから呼び出され、おそらくアプリケーションの起動時にはOWINフレームワークで検索される属性でマークされていますファイルpartialは、Startupクラスを拡張します。 Startup.Auth.cs

が認証オプションを設定するためのクラスだと通常

public partial class Startup 
{ 
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
    public void ConfigureAuth(IAppBuilder app) 
    { 
     // Enable the application to use a cookie to store information for the signed in user 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login"), 
      CookieSecure = CookieSecureOption.Always 
     }); 
     // Use a cookie to temporarily store information about a user logging in with a third party login provider 
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     // .... 
     // I deleted code which is commented out if you selected "Individual accounts" 
     // if you created the site project using the VS 2013 wizard 
     // ... 
    } 
} 

は私が署名されたクッキーを確保するための CookieSecureオプションを追加し、次のコードを持っており、それは、それ以外の、優れたセキュリティプラクティスとして推奨されてボイラープレートコード。

さらに詳しい文書をCookieAuthenticationOptionsに掲載しています。

+1

ウェブサイト全体がHTTPSであれば、 'CookieSecureOption.Always'を使うのは冗長です。さらに、ドキュメンテーションから、ローカル開発にもHTTPSを使用する必要があります。デフォルトでは 'CookieSecureOption.SameAsRequest'です。したがって、あなたのウェブサイトがHTTPSにある場合、デフォルト設定が十分安全であるため、 'CookieSecure'プロパティを変更しないことをお勧めします。 – QuantumHive

2

Sandeep Phadke氏によると、startupUt.csの設定のためにreturnUrlパラメータが満たされています。

CookieAuthenticationOptionsには、デフォルトで「returnUrl」に設定されているReturnUrlParameterというプロパティがあります。それが魔法のように見える理由です。あなたが好きにそれを変更することができます。

app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     LoginPath = new PathString("/Account/Login"), 
     ReturnUrlParameter = "returnTo" 
    }); 

次にあなたがAccountControllerログイン・アクションを変更することができます。

[AllowAnonymous] 
    public ActionResult Login(string returnTo) 
    { 
     ViewBag.ReturnUrl = returnTo; 
     return View(); 
    } 
関連する問題