2011-12-22 4 views
1

私はこの問題を徹底的に取り上げました。基本的には、ユーザーが認証されても適切なアクセス権がない場合、403エラーをスローするカスタムAuthorizeAttributeを持つMVCページがあります。私が抱えている問題は、このエラーをカスタムコントローラ/アクション(/ Error/Unauthorized)にリダイレクトしたいということです。IIS7.5 HttpErrors ExecuteURLが実行されていません

私は上記の構成によると、私のweb.config

<httpErrors errorMode="Custom"> 
    <remove statusCode ="403" subStatusCode="-1"/> 
    <error statusCode="403" path="/Error/Unauthorized" responseMode="ExecuteURL" /> 
</httpErrors> 

に以下を追加している、私はデフォルトは7.5 403リダイレクトをIIS表示されません。しかし、私は何も見ません。 IEでは、ウェブサイトにログインする必要があり、クロムは空白のページを表示するだけです。

アイデア?ここで

public class CustomAuthorize : AuthorizeAttribute 
    { 
     //Property to allow array instead of single string. 
     private string[] _authorizedRoles; 

     public string[] AuthorizedRoles 
     { 
      get { return _authorizedRoles ?? new string[0]; } 
      set { _authorizedRoles = value; } 
     } 

     protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
     { 
      base.HandleUnauthorizedRequest(filterContext); 
      if (filterContext.HttpContext.Request.IsAuthenticated) 
      { 
       filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
       filterContext.Result = new HttpStatusCodeResult(403); 
      } 
     } 

     protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 
      if (httpContext == null) 
       throw new ArgumentNullException("httpContext"); 

      if (!httpContext.User.Identity.IsAuthenticated) 
       return false; 

      //Check to see if any of the authorized roles fits into any assigned roles only if roles have been supplied. 
      if (AuthorizedRoles.Any(httpContext.User.IsInRole)) 
       return true; 

      return false; 
     } 
    } 
+0

OK、私は(これは私が想定しTrySkipIisCustomErrorsに)、system.webセクションに私のcustomErrorsセクションを追加し、この場合、404でこれを試してみました、I私はhttpErrorsセクションも必要ないことを発見しました。ただし、403を追加しようとすると、そのページは解決しません。これはブラウザが403をどのように解釈しているのでしょうか?彼らは403がリダイレクトされるのを許可しませんか? –

答えて

0

OKを助けるかもしれない場合は、カスタム認証コードで、私はこれが本当に正しいかどうかわからないが、それは私の症状に適合します。 http://forums.asp.net/t/1462153.aspx/1 私はリダイレクトをコード化しなければならないのは不幸ですが、今後のメンテナンス性のために少なくとも明示的にしようとしました。ただ、いくつかの異なるシナリオで遊んしようとして

public bool RedirectAuthenticatedButUnauthorizedUsers { get; set; } 

    private String _redirectUnauthorizedUrl = String.Empty; 
    public String RedirectUnauthorizedUrl 
    { 
     get { return _redirectUnauthorizedUrl; } 
     set { _redirectUnauthorizedUrl = value.Trim(); } 
    } 

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     base.HandleUnauthorizedRequest(filterContext); 
     if (!RedirectAuthenticatedButUnauthorizedUsers || !filterContext.HttpContext.Request.IsAuthenticated) 
      return; 
     if(RedirectUnauthorizedUrl == String.Empty) 
      throw new NullReferenceException("RedirectAuthenticatedButUnauthorizedUsers " + 
              "set to true, but no redirect URL set."); 
     filterContext.HttpContext.Response.Redirect(RedirectUnauthorizedUrl); 
    } 
関連する問題