C# ASP MVC5-标识。如何获取当前ApplicationUser并使用此用户查询以用户作为外键的表

C# ASP MVC5-标识。如何获取当前ApplicationUser并使用此用户查询以用户作为外键的表,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个ASP.NETMVC应用程序。我使用个人用户帐户和标识API的方式是,只有canEdit角色中的用户才能编辑数据(我的数据是产品)。ASPNetUsers和Products实体有1:N关系(这只是一个示例)。因此,用户拥有一个产品列表 当用户请求http://localhost:1111/Products页面应显示该用户以前登录的产品列表 我不知道如何获取当前ApplicationUser以及如何查询products表以获取属于某个用户的产品列表 这是我的ProductsControll

我有一个ASP.NETMVC应用程序。我使用个人用户帐户和标识API的方式是,只有canEdit角色中的用户才能编辑数据(我的数据是产品)。ASPNetUsers和Products实体有1:N关系(这只是一个示例)。因此,用户拥有一个产品列表

当用户请求
http://localhost:1111/Products
页面应显示该用户以前登录的产品列表

我不知道如何获取当前ApplicationUser以及如何查询products表以获取属于某个用户的产品列表

这是我的ProductsController的代码:

public class ProductsController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();


    // GET: Products
    [AllowAnonymous]
    public ActionResult Index()
    {
        string currentUserId = User.Identity.GetUserId();
        ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);

        //How to modify these sentence to get the products of the current user??
        Product product = db.Products.Find();
        return View(db.Products.ToList());
    }
....

由于您使用的是个人身份验证,因此您应该能够使用用户ID直接从应用程序数据库访问用户:

UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
要获取当前登录的用户,应使用登录用户的详细信息自动填充
user
对象

由于您具有
[AllowAnonymous]
属性,您还必须确保用户实际使用
请求登录。IsAuthenticated
否则用户对象将为空

将其全部添加在一起将使您的代码看起来像:

CurrentUser = UserManager.FindById(User.Identity.GetUserId());
[AllowAnonymous]
public ActionResult Index()
{
    if (Request.IsAuthenticated)
    {
        UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
        ApplicationUser currentUser = UserManager.FindById(User.Identity.GetUserId());

        // assuming there is some kind of relationship between products and users
        List<Product> products = db.Products.Where(p => p.User.Equals(currentUser.UserID)).ToList(); // or .email or other field from your users table

        // OPTIONAL: Make sure they see something
        if(products.Count ==0) // They have no related products so just send all of them
            products = db.Products.ToList();

        // only send the products related to that user
        return View(products);
    }
    // User is not authenticated, send them all products
    return View(db.Products.ToList());
}
[AllowAnonymous]
公共行动结果索引()
{
如果(请求已验证)
{
UserManager UserManager=newusermanager(newuserstore(db));
ApplicationUser currentUser=UserManager.FindById(User.Identity.GetUserId());
//假设产品和用户之间存在某种关系
列出products=db.products.Where(p=>p.User.Equals(currentUser.UserID)).ToList();//或.email或用户表中的其他字段
//可选:确保他们看到了什么
如果(products.Count==0)//它们没有相关的产品,只需发送所有产品即可
products=db.products.ToList();
//仅发送与该用户相关的产品
返回视图(产品);
}
//用户未通过身份验证,请向他们发送所有产品
返回视图(db.Products.ToList());
}

首先,检查产品类中是否有用户ID,如果有,然后:

string currentUserId = User.Identity.GetUserId();

var userProducts=db.products.Where(p=>p.UserId==currentUserId).Tolist();

//you need to replace in my code the UserId with the name of the user id from your products table. 
返回视图(db.Products.ToList())的istead只需编写
返回视图(userProducts)

…就像Jake说的:删除属性
[AllowAnonymous]

试试这个

 Product product = CurrentUser.Products;