C#返回登录用户的数据
我使用ASP.NETCore3作为会员。目前,我使用index方法返回资产表中的所有条目。我想更改它,以便它根据登录用户返回资产,同时允许管理员角色查看所有资产 在Assets表中,我有一列,其中存储了创建新资产的每个用户的用户ID。我不知道用我已经有的东西该怎么办 控制器代码C#返回登录用户的数据,c#,asp.net-core,.net-core,C#,Asp.net Core,.net Core,我使用ASP.NETCore3作为会员。目前,我使用index方法返回资产表中的所有条目。我想更改它,以便它根据登录用户返回资产,同时允许管理员角色查看所有资产 在Assets表中,我有一列,其中存储了创建新资产的每个用户的用户ID。我不知道用我已经有的东西该怎么办 控制器代码 public ViewResult Index() { // retrieve all the assets var model = _assetRepository.GetAllAssets();
public ViewResult Index() {
// retrieve all the assets
var model = _assetRepository.GetAllAssets();
// Pass the list of assets to the view
return View(model);
}
public IEnumerable<Asset> GetAllAssets() {
return context.Asset;
}
public interface IAssetRepository {
Asset GetAsset(int Id);
IEnumerable<Asset> GetAllAssets();
Asset Add(Asset asset);
Asset Update(Asset assetChanges);
Asset Delete(int Id);
}
应答代码
public ViewResult Index() {
// retrieve all the assets
var model = _assetRepository.GetAllAssets();
// Pass the list of assets to the view
return View(model);
}
public IEnumerable<Asset> GetAllAssets() {
return context.Asset;
}
public interface IAssetRepository {
Asset GetAsset(int Id);
IEnumerable<Asset> GetAllAssets();
Asset Add(Asset asset);
Asset Update(Asset assetChanges);
Asset Delete(int Id);
}
public IEnumerable GetAllAssets(){
返回上下文。资产;
}
接口代码
public ViewResult Index() {
// retrieve all the assets
var model = _assetRepository.GetAllAssets();
// Pass the list of assets to the view
return View(model);
}
public IEnumerable<Asset> GetAllAssets() {
return context.Asset;
}
public interface IAssetRepository {
Asset GetAsset(int Id);
IEnumerable<Asset> GetAllAssets();
Asset Add(Asset asset);
Asset Update(Asset assetChanges);
Asset Delete(int Id);
}
公共接口IAssetRepository{
资产获取资产(内部Id);
IEnumerable GetAllAssets();
资产增加(资产);
资产更新(资产变更);
资产删除(int Id);
}
一个很好的起点是遵循其中一个关于在ASP.NET Core中使用身份验证和授权的教程。这似乎解决了您的问题。一个很好的起点是遵循其中一个教程,介绍如何在ASP.NET Core中使用身份验证和授权。这似乎解决了您的问题。首先,我建议您添加一个新列,保留“角色”。如果您不想使用已经这样做的框架。然后,如果您添加了一个列,其中包含您可以在存储库中执行的角色,如下所示:
public IEnumerable<Asset> GetAllAssets(Guid userId) {
var role = context.Asset.Where(x => x.userId = userId).Select(x => x.role);
if (role = "admin")
return context.Asset;
return context.Asset.Where(x => x.userId = userId);
}
public IEnumerable GetAllAssets(Guid用户ID){
var role=context.Asset.Where(x=>x.userId=userId)。选择(x=>x.role);
if(role=“admin”)
返回上下文。资产;
返回context.Asset.Where(x=>x.userId=userId);
}
对于这种情况,我硬编码“admin”,您可以用您的实现替换它。但我认为你想找到这样的解决方案
我希望能帮助你。我建议您在控制器和存储库之间添加一个新的层“服务”,您可以在那里添加“业务逻辑”。首先,我建议您添加一个保留“角色”的新列。如果您不想使用已经这样做的框架。然后,如果您添加了一个列,其中包含您可以在存储库中执行的角色,如下所示:
public IEnumerable<Asset> GetAllAssets(Guid userId) {
var role = context.Asset.Where(x => x.userId = userId).Select(x => x.role);
if (role = "admin")
return context.Asset;
return context.Asset.Where(x => x.userId = userId);
}
public IEnumerable GetAllAssets(Guid用户ID){
var role=context.Asset.Where(x=>x.userId=userId)。选择(x=>x.role);
if(role=“admin”)
返回上下文。资产;
返回context.Asset.Where(x=>x.userId=userId);
}
对于这种情况,我硬编码“admin”,您可以用您的实现替换它。但我认为你想找到这样的解决方案
我希望能帮助你。我建议您在控制器和存储库之间添加一个新的层“服务”,您可以在那里添加“业务逻辑”。在来到这里之前,我已经检查过了。这个例子和其他例子的问题是,它仍然会拉入所有数据,并将编辑/删除功能限制为登录用户。我需要只显示在列表中创建的用户的数据。我还需要能够使用上面的代码。@oscararango该链接应该适合您。对用户进行身份验证后,请使用
context.user.UserID
并根据它筛选资产。在来到这里之前,我检查了这一点。这个例子和其他例子的问题是,它仍然会拉入所有数据,并将编辑/删除功能限制为登录用户。我需要只显示在列表中创建的用户的数据。我还需要能够使用上面的代码。@oscararango该链接应该适合您。对用户进行身份验证后,使用context.user.UserID
并根据该属性筛选资产。