2013-09-06 3 views
10

私はasp.netの簡単なWebサイトで作業しています。 私は、特定のADグループのユーザーだけが許可されるように、サイドへのアクセスを制限したいと思います。私はそれをして、それは正常に動作しています。しかし、ADグループに所属していないユーザーがサイトにアクセスしようとすると、ログインプロンプトが表示されます。ログインプロンプトを表示するのではなく、権限のないユーザーをカスタムページにリダイレクトするにはどうすればよいですか?許可されていないユーザーをasp netにリダイレクト

以下は私のweb.configです。コードの一番下の部分は、私が試したが動作しなかったものです。

<configuration> 
<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <authentication mode="Windows"/> 
    <authorization> 
    <allow roles="DOMAIN\GROUP"/> 
    <deny users="*"/> 
    </authorization> 
</system.web> 

<location path="AccessDenied.aspx"> 
<system.web> 
<authorization> 
    <allow users="*"/> 
</authorization> 
</system.web> 
</location> 
</configuration> 

私はGlobal.asax.csにこれを追加しました:

protected void Application_EndRequest(Object sender, EventArgs e) 
    { 
     if (HttpContext.Current.Response.Status.StartsWith("401")) 
      { 
       HttpContext.Current.Response.ClearContent(); 
       Server.Execute("AccessDenied.aspx"); 
      } 
} 

任意のアイデア?

編集: 投稿されたソリューションのいくつかを試しましたが、機能しませんでした。 しかし、私はそれは、このコードで作業しました:

void Application_EndRequest(object sender, System.EventArgs e) 
    { 
     if (((Response.StatusCode == 401) 
     && (Request.IsAuthenticated == true))) 
     { 
      Response.ClearContent(); 
      Response.Redirect("~/AccessDenied.aspx"); 
     } 
    } 
} 
+0

'<許可ユーザー=「*」/>は'どのユーザーがアクセスを許可されている意味するので、あなたが特定のグループやユーザーを許可する必要があります。 –

+0

はい、何らかの理由でweb.configの一部が表示されませんでした。 – mads

+0

'Propmt'は黄色のエラーページを意味します。 –

答えて

3

あなたはResponse.RedirectまたはServer.Transfer

Response.Redirect("AccessDenied.aspx"); 

全例を使用することができます:あなたがすべて処理したいと仮定すると、

protected void Application_EndRequest(Object sender, EventArgs e) 
{ 
    if (HttpContext.Current.Response.Status.StartsWith("401")) 
    { 
     HttpContext.Current.Response.ClearContent(); 
     Response.Redirect("AccessDenied.aspx"); 
    } 
} 
2

「不正な」エラー:

<customErrors defaultRedirect="Error.aspx" mode="On"> 
    <error statusCode="401" redirect="Unauthorized.aspx" /> 
    <error statusCode="403" redirect="Forbidden.aspx" /> 
</customErrors> 

401(許可されていない)リクエストはすべてUnauthorized.aspxに転送されます。

+2

これはうまくいかないが、うまくいかない。マイクロソフトはデザインで面白い決定を下すことがあります。 –

+0

UrlAuthorizationModuleから返された '401 Unauthorized'がFormsAuthenticationModuleによって '302 Redirect'に変換されるため、これは機能しません。http://www.asp.net/web-forms/overview/older-versions- – track0

0
<authorization> 
<!--<allow users="*"/>-->This here means allow everyone . 
<allow users="AD"/> -- Add this group to AD domain . 
<deny users="?"/> --Deny unknown users(Not authenticated) 
<allow roles="Admins"/> --If you have created roles .  

あなたが使用<allow user ="AD">よりローカルグループを持っていますが、ADドメインにそれを登録する必要があります。 <allow roles ="AD" />は、ローカルグループではないドメインADグループでのみ動作します。

protected void Application_EndRequest(Object sender,EventArgs e) 

    { 
    HttpContext context = HttpContext.Current; 
    if (context.Response.Status.Substring(0,3).Equals("401")) 
    { 
     context.Response.ClearContent(); 
     //do redirect here 
    } 
    } 
+0

'allow user'は、ADグループではなく、ユーザアカウント名のためのものです。 – Nicholas

+0

@Nicholasもしあなたがを使うよりもローカルグループを持っていて、それをADドメインに登録しなければならないならば、 は、ローカルグループではなくドメインADグループでのみ機能します。「このステートメントは読者を混乱させて別の方向に導いてくれるかもしれませんが、指摘してくれてありがとうまで、より良い言葉でそれを変更するかもしれませんが、私は「あなたがActive Directoryのためのユーザーです」と言っているわけではありません。 –

+0

'allow roles'を使用してロールマネージャを' WindowsTokenRoleProvider'に設定すると、サーバがドメインのメンバであれば、ローカルコンピュータグループとドメインセキュリティグループの両方が検出されます。 https://msdn.microsoft.com/en-us/library/wce3kxhd%28v=vs.100%29.aspxを参照してください。 – Nicholas

1

私はこれでより多くの成功を収めて:

 // This is a workaround for the fact that we are not using MVC and its attributes 
     // This is the situation where a user is logged in - but not authorized for this page 
     void Application_EndRequest (object sender, System.EventArgs e) 
     { 
     if (((Response.StatusCode == 302) && (Request.IsAuthenticated == true))) 
     { 
      try 
      { 
       string sLoc = Response.Headers ["Location"]; 
       if (sLoc.Contains ("Login")) 
       { 
        Response.ClearContent(); 
        Response.Redirect ("~/AccessDenied.aspx"); 
       } 
      } 
      catch 
      { 
      } 
     } 
     } 
関連する問題