我有一个带有存储库层的 Windows 认证 MVC 应用程序。控制器与数据库的所有交互都是通过存储库完成的。每个控制器都有一个对存储库的引用:
public class PostController : Controller
{
    private Repository db = new Repository();
    [HttpPost]
    public ActionResult DeletePost(int id)
    {
        // Authorize that the user is allowed to delete this post...
        db.DeletePost(id);
    }
}
我的问题是是否有一种将我的授权逻辑移动到存储库层的好方法。我希望该Repository.DeletePost()功能拒绝删除不是由经过身份验证的用户创建的帖子。问题是我的存储库不知道经过身份验证的用户是谁。控制器知道(通过Controller.User)。
将 传递给Controller.User构造Repository函数不起作用,因为在Controller.User调用构造函数时显然没有定义。
如何告知经过Repository身份验证的用户是谁?最好只Repository在每个动作中构建?或者在存储库层处理它是一个坏主意?