2009-07-28 16 views
0

ユーザーが認証されていない場合、ログインページにリダイレクトされるすべてのコントローラが継承するクラスを実装しています。 RedirectToAction行はリダイレクトされません。あなたはplesaeが正しいことを助けることができますか?MVCリダイレクトの問題

public class SecureController : Controller 
    { 
     public SecureController() 
     { 
      if (User == null || !User.Identity.IsAuthenticated) 
      { 
       RedirectToAction("Logon", "Account"); 
      } 
     } 
    } 

答えて

1

代わりにActionFilterを使用することをお勧めします。はるかに簡単です。これは、あなたがちょうどそうのようなあなたのコントローラであなたのアクションメソッドに属性を置くようになる

public class RequiresAuthenticationAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     // You can do any custom authentication logic in here 

     if (User == null || !User.Identity.IsAuthenticated) 
     { 
      // This assumes that Account is the controller, and Logon is the action method. 
      // You might want to check your routes if this is still not working correctly 
      RedirectToAction("Logon", "Account"); 
     } 
    } 
} 

[RequiresAuthentication] 
public ActionResult Index() 
{ 
    return View(); 
} 
あなたならば他の人が指摘したように

それとも、あなたは、このような何かを行うことができますカスタム認証ロジックを必要としない、あなただけのAuthorizeAttributeを使用することができます。すでに組み込まれてい

[Authorize] 
public ActionResult Index() 
{ 
    return View(); 
} 
+2

それはCACHをサポートとして、また、これより良い作品属性 – terjetyl

+0

を認可を参照してください。 ing。 –

+0

私は彼が自分の方向に向かっていたので、カスタム認証をする必要があると思っていました。これは私が使用する属性のタイプです。私はすべてのカスタム認証ロジックを置き換えました。 – mkchandler

関連する問題