gpt4 book ai didi

c# - 确保用户 X 不查询用户 Y 的数据

转载 作者:行者123 更新时间:2023-11-30 16:11:19 25 4
gpt4 key购买 nike

假设我有一个 Organisation其中有很多Student的。每个 Organisation 都有一个登录名其中显示了相关 Student 的列表的。

查看 Student 的列表时的 我可以通过单击其中一个来编辑它们。这将我带到一个页面让我们说 /students/edit/<id> .

查询此页面的 View 可以从我的服务层获取学生:

public class StudentService : IStudentService {

private readonly IRepository _repository; // injected in constructor

public Student GetStudent(int id) {
return _repository.Get<Student>(x => x.Id = id);
}
}

我的问题是:

  1. 如何确保组织 X 不查询组织 Y 的数据(例如,通过在查询字符串中使用不属于该登录组织的不同 ID)?

  2. 服务层是否应该添加额外的连接 (&& x.OrganisationId == <organisation id>) 以确保当前登录Organisation是唯一一个数据是查询自?如何从服务层获取当前用户?

  3. 我应该向 GetStudent(int id, int organisationId) 添加一个额外的参数吗?让 Controller 设置它?这似乎只会让我的服务膨胀。

我希望我已经解释清楚了,如果我对自己感到困惑,请告诉我 ;-)

最佳答案

我建议让您的服务知道当前经过身份验证的用户。这将允许您在每个查询中包含类似于以下内容的限制性 Where 子句,以防止用户看到其他组织的学生

 private Student GetStudent(int id) {
return _repository
.Get<Student>(x => x.Id = id)
.Where(s => s.OrganisationId == _currentUser.OrganisationId)
.FirstOrDefault();
}

关于c# - 确保用户 X 不查询用户 Y 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24483789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com