1

MVC 3 プロジェクトのリソースに対してユーザーを承認する必要があります。次のようにするのは良い習慣ですか?

interface IResource
{
    string Id { get; set; }
    bool Authorize(User user);
}

class User
{
    // some logic for user
}

class ResourceA : IResource
{
    public string Id { get; set; }

    public ResourceA(string id)
    {
        Id = id;
    }

    bool Authorize(User user)
    {
        // Query database to check if user has permission to the resource
        // return true if the user has, otherwise false
    }
}

class TestController : Controller
{
    // Get details of resource A
    public ActionResult Get(User user, string resourceId)
    {
        IResource resource = new ResourceA(resourceId);
        if (resource.Authorize(user))
        {
            // do stuff
        }
        else
        {
            throw HttpException(403, "No permission");
        }
    }
}

上記の実装では:

Web サイトのすべてのリソースは、強制的に IResource インターフェイスを実装します。

リソースと対話するすべてのリクエストは、承認コードを実装する必要があります。

どんな助けでも大歓迎です。

4

1 に答える 1

0

解決策は良さそうです。resource に関連する特定のコントローラーとアクションがある場合は、 ActionFilter を使用して、カスタム ロジックを作成し、検証が失敗したかどうかを確認してリダイレクトすることもできます。属性は、中央論理システムの方法の 1 つでもあります。解決策は、ユーザーがリソースと対話する方法によって異なります。

于 2013-03-27T06:20:01.580 に答える