0

アプリケーションのさまざまなセクションへのアクセスを制御する正しい方法を見つけようとしています。

私のアプリには 3 つのセクションがあります。

  1. 管理者
  2. スーパーユーザー
  3. 一般ユーザー

http://blogs.msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspxを読んだので、持っていても理解していますそれぞれに領域がありますが、それを承認に使用したくありません。

私の考えは、セクションごとに 1 つずつ、3 つの基本コントローラー クラスを持つことです。AdminBaseController、、、SuperUserBaseControllerのようなものRegularUserBaseController

その後、それぞれに を追加できることはわかってAuthorizeAttributeいますが、必要なロールを設定に保存したいので、それらを属性に設定できません。

だから私は を継承しAuthorizeAttributeてオーバーライドする必要があると考えていますOnAuthorization、そしてこれが私が立ち往生している場所です。これは私がこれまでに持っているものです。

    public override void OnAuthorization(HttpActionContext actionContext)
    {
        if (actionContext.ControllerContext.Controller is AdminBaseController)
        {
            //do something
        }
        else if (actionContext.ControllerContext.Controller is SuperUserBaseController)
        {
            //do something
        }
        else if (actionContext.ControllerContext.Controller is RegularUserBaseController)
        {
            //do something
        }
        else
        {
            //someone forgot to use a base controller
            //deny be default
        }
    }

RolesandUsersプロパティを正しい値に設定してから、最後に呼び出すだけだと思いますbase.OnAuthorization。これは合理的な解決策のように思えますか? また、すべてを拒否するには、両方のプロパティを に設定する必要があります""か?

私が道を外れている場合は、より良い方向に向けてください。

4

1 に答える 1

3

Fluent Security http://www.fluentsecurity.net/を見てください。

.NET に組み込まれているセキュリティ機能よりもずっと気に入っています。サンプルには、役割ベースのアクセス許可の例があります。また、あなたがやろうとしていることよりもきれいです。

Fluent Security を使用してサイトのセキュリティを構成する方法のサンプルを次に示します。

/// <summary>
/// Configuration Helper for Fluent Security. See http://www.fluentsecurity.net
/// </summary>
public static class SecurityConfig
{
    public static void Configure()
    {
        SecurityConfigurator.Configure(c =>
        {
            c.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
            c.GetRolesFrom(() => (HttpContext.Current.Session["Roles"] as string[]));

            // Blanket Deny All
            c.ForAllControllers().DenyAnonymousAccess();                

            // Publicly Available Controllers
            c.For<HomeController>().Ignore();
            c.For<RegistrationsController>().Ignore();
            c.For<LoginController>().Ignore();

            // Only allow Admin To Create
            c.For<ReservationsController>(x => x.Create())
             .RequireRole(UserRoles.Admin.ToString());

            c.For<ReservationsController>(x => x.Edit(""))
             .RequireRole(UserRoles.Admin.ToString(),UserRoles.User.ToString());

            c.For<ReservationsController>(x => x.Delete(""))
             .RequireRole(UserRoles.Admin.ToString(),UserRoles.User.ToString());           
        });
    }
}
于 2013-01-30T18:43:33.657 に答える