3

ASP.NET を使用していますが、ユーザーを Web 構成から別のページにリダイレクトできるようにしたいと考えています。

次のような制限があります。

 <location path="Structures.aspx">
    <system.web>
      <authorization>
        <allow roles="Admin"/>
        <deny users="*" />
      </authorization>
    </system.web>
  </location>

そして、ユーザーをあるページにリダイレクトすると素晴らしいでしょう。この投稿を見まし たが、探していたものではありません。

コードビハインドではなく、web.configで行う必要があります。ありがとう!

4

2 に答える 2

5

すべての "Unauthorized" エラーを処理したい場合:

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

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

または、イベントでチェックを実行する必要がありますPage_Load。これが面倒だと思われる場合は、管理者専用であると想定されているすべてのページの基本ページ クラスをいつでも作成し、そこでチェックを実行できます。例えば

// base class
public class AdminOnlyPage : Page
{
  /*...*/ Page_Load(Object sender, EventArgs e)
  {
    /* check if the user is admin otherwise reject and redirect */
  }
}

// Your "Structures.aspx" page
public class Structures : AdminOnlyPage
{
}
于 2013-02-06T14:45:33.020 に答える
1

「Location」ヘッダーが設定された「302 Found」コードを使用して、アプリがログイン ページにリダイレクトされていることに気付きました。私のログインページはたまたま同じサーバーを共有する外部アプリケーションにあるため、変更できませんでした。

代わりに、これを global.asax に追加しました。

    protected void Application_EndRequest(Object sender, EventArgs e)
    {
        if (Response.Status.StartsWith("302") 
            &&
            Request.IsAuthenticated 
            &&
            Response.RedirectLocation.StartsWith(System.Web.Security.FormsAuthentication.LoginUrl))
        {
            //log.Trace("Preventing redirection from app to login form since user is already logged in. It's authorization issue, not authentication.");
            Response.Clear();
            Response.Redirect("~/AccessDenied.aspx");
        }
    }
于 2016-04-12T19:12:41.803 に答える