6

ユーザーがアクセス権のないエンティティにアクセスできないように、コントローラー アクションを保護しようとしています。次のコードでこれを行うことができます。

public ActionResult Entity(string entityCode)
{
    if (CurrentUser.VerifyEntityPermission(entityCode))
    {
        //populate viewModel...
        return View(viewModel);
    }
    return RedirectToAction("NoAccessToEntity", "Error");
}

コントローラー アクション自体に属性を追加できるようにしたいと考えています。エンティティへのアクセスを検証するには、コントローラーに渡された値と、ユーザーがアクセスできるエンティティを確認する必要があります。これは可能ですか?

[EntityAuthRequired]
public ActionResult Entity(string entityCode)
{
        //populate viewModel...
        return View(viewModel);
}
4

1 に答える 1

3

このようなものが途中で役立つかもしれません。ただし、ハードコードするのではなく、各アクションで entityCode パラメーターを指定できるようにするために、属性にいくつかのプロパティを追加することをお勧めします。

public class EntityAuthRequired : FilterAttribute, IAuthorizationFilter 
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        //Make sure that this is not NULL before assigning value as string...
        var entityCode = filterContext.RouteData.Values["entityCode"] as string;
        // do your logic...         
        if (!allowed)
            filterContext.Result = new HttpUnauthorizedResult();            
    }
}

また、entityCodeRouteData にない場合はfilterContext.RequestContext.HttpContext.Request、POST データを参照するために使用できます。

于 2010-05-12T21:28:15.223 に答える