我正在一个项目中工作,我想在更新或删除之前检查用户ID。我使用Entity Framework和ASP.NET MVC。
表1是
public class User {
public int UserId { get; set; }
// more stuff here.
public ICollection<UserPost> UserPost{get; set;}
}
这是我的第二张表
public class Post {
public int PostId { get; set; }
// more stuff
public ICollection<UserPost> UserPost{get; set;}
}
这是我的最后一个:
public class UserPost {
public int userPostId { get; set; }
public int userId {get; set; }
public int postId {get; set; }
public virtual User User { get; set; }
public virtual User Post{ get; set; }
// and more
}
在我看来,我想检查用户是否更新或删除他的帖子,如此代码,但它不起作用。
@model List<MyFinalProject.Core.Domain.Post>
// Razor syntax here
@if (User.Identity.IsAuthenticated && User.IsInRole("Admin") ||
Model[i].UserPost.Where(u => u.UserId == User.Identity.GetUserId<int>()))
{ // some logic }
答案 0 :(得分:1)
您是否已将用户的身份服务添加到 Startup.cs 的 ConfigureServices()?
有关详情 AspNet.Identity ,请查看此信息:
https://docs.asp.net/en/latest/security/authentication/introduction-to-aspnet-identity.html
答案 1 :(得分:1)
非常感谢你的帮助,我刚刚找到答案,我想与你分享。我只将(||)条件后的第二部分改为:
@if (User.Identity.IsAuthenticated && User.IsInRole("Admin") ||
Model[i].UserPost.Any(e => e.UserId == User.Identity.GetUserId<int>()))