2009-09-14 9 views
10

私はこのような承認とコントローラ属性いますHttp 401の結果に対してカスタムエラーページを表示するにはどうすればよいですか?

[Authorize(Roles = "Viewer")] 
public class HomeController : Controller 
{ 
    //... 
} 

と私のweb.configファイルには、以下のような設定のcustomErrorsを持っています

<customErrors mode="On"> 
     <error statusCode="401" redirect="notauthorized.html"/> 
    </customErrors> 

私はホームコントローラのアクションを呼び出すしようとすると、許可されていない役割を使用すると、空白のページが表示されます。私はカスタムページにリダイレクトされません。 アイデア私の知る限りでは

答えて

0

標準的なアプローチは、着信要求を処理し、コードが返されたはhttpStatusに応じて、適切なビューを出力し、単純なエラーコントローラを持つことです...このような何か:

public class ErrorController : Controller 
{ 

    [AcceptVerbs(HttpVerbs.Get)] 
    public ViewResult Index() 
    { 

     //Check if the statuscode is HttpStatusCode.NotFound; 
     if(Response.StatusCode == 401) 
      return View("NotAuthorised"); 
     return View(); 
    } 
} 

とその後、あなたのWebConfigにリダイレクトアクションを指定します。

<customErrors mode="On" defaultRedirect="~/Error" /> 
3

は、this very similar questionからtvanfossonさんAnswerを見てこれは私が(tvanfossonに感謝を)やっているものですので、今私は言っている:

[MyAuthorize(Roles="SuperAdmin",ViewName="AccessDenied")] 
public class SuperAdminController : Controller 
... 

ユーザーがロールに属していない場合、ユーザーはViewNameで指定されたビューを取得します。

注:空白のページがカッシーニから来ている、あなたは実際のIISサーバーにアプリを移動する場合は、401

+0

のようなあなたのアクションコントローラであなたの「フラグ」を追加カスタムページ?または私はちょうど標準のIIS 401のページを参照する必要がありますか? –

+0

401が表示されます。 –

9

私はこの質問に感謝は少し古いですが、これは、誰かを助けることが表示されます。

401の場合は、web.configのcustomerrorsセクションに401を追加した場合でも、おそらく標準の401 Unauthorizedページが表示されます。私は、IISとWindows認証を使用すると、ASP.NETがリクエストを見る前にチェックが行われるので、カッシーニとIIS上の空白のページが表示されることを理解しています。

私のプロジェクトでは、Global.asaxファイル401エラーのために作成したルートにリダイレクトし、ユーザーに「この権限がないことを確認する」ビューを送信します。 Global.asaxの中

void Application_EndRequest(object sender, System.EventArgs e) 
    { 
     // If the user is not authorised to see this page or access this function, send them to the error page. 
     if (Response.StatusCode == 401) 
     { 
      Response.ClearContent(); 
      Response.RedirectToRoute("ErrorHandler", (RouteTable.Routes["ErrorHandler"] as Route).Defaults); 
     } 
    } 

とRoute.configで:

 routes.MapRoute(
     "ErrorHandler", 
     "Error/{action}/{errMsg}", 
     new { controller = "Error", action = "Unauthorised", errMsg = UrlParameter.Optional } 
     ); 

とコントローラで:

public ViewResult Unauthorised() 
    { 
     //Response.StatusCode = 401; // Do not set this or else you get a redirect loop 
     return View(); 
    } 
0

また、あなたがあなた自身を作成することができますカスタム承認属性とyのユーザをリダイレクトする独自のルートを設定します私たちのページ。

ここに私のプロジェクトのサンプル:

/*../controllers/CustomAuthorizationAttribute.cs */ 
public class CustomAuthorizationAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext) 
    { 
     string session = filterContext.HttpContext.Session["id"] != null ? filterContext.HttpContext.Session["id"].ToString() : null; 
     if (string.IsNullOrEmpty(session)) 
     { 
      // Unauthorized! 
      filterContext.Result = new RedirectToRouteResult(
       new RouteValueDictionary 
       { 
        { "action", "Create" }, { "controller", "Sessions" } 
        //,{ "parameterName", "YourParameterValue" } 
       } 
      ); 
     } 
    } 
} 

と私はIISにアプリを移動する場合、私は見るべきあなたは意味この

/*../controllers/ReportsController.cs */ 
public class ReportsController : Controller 
{ 
    [CustomAuthorizationAttribute] 
    public ActionResult Index() 
    { 
     //do something 
    } 
} 
関連する問題