【问题标题】:ASP MVC5 - Identity. How to get current ApplicationUser and use this user to query a table with user as foreign keyASP MVC5 - 身份。如何获取当前 ApplicationUser 并使用此用户查询以用户为外键的表
【发布时间】:2016-03-10 01:40:13
【问题描述】:

我有一个 ASP.NET MVC 应用程序。我使用个人用户帐户和身份 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());
    }
....

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

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

    UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
    

    这将使用您数据库中的用户创建一个对象,然后您可以简单地使用以下方式调用它:

    CurrentUser = UserManager.FindById(User.Identity.GetUserId());
    

    要获取当前登录的用户,User 对象应自动填充登录用户的详细信息。

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

    将它们加在一起将使您的代码看起来像:

    [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());
    }
    

    【讨论】:

      【解决方案2】:

      首先,检查Products类中是否有UserId,如果有,则:

      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. 
      

      你只需要写return View(userProducts);而不是return View(db.Products.ToList());

      ...就像杰克说的:删除属性[AllowAnonymous]

      【讨论】:

        【解决方案3】:

        试试这个

         Product product = CurrentUser.Products;
        

        【讨论】:

          猜你喜欢
          • 2016-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-22
          • 1970-01-01
          • 1970-01-01
          • 2013-08-29
          相关资源
          最近更新 更多