I have a model with a method and I want to get the current UserID and have access to the database context.
public class Annonce
{
public int ID { get; set; }
...
public bool AnnonceDansFavoris(UserManager<ApplicationUser> UserManager)
{
// Recherche du user ID courant
string userID = UserManager.GetUserId(ClaimTypes.NameIdentifier);
var _context = (PatrimoineClickDbContext)ValidationContext.GetService(typeof(ApplicationDbContext));
return (_context.Favoris.Any(f => (f.CreePar == userID) && (f.AnnonceID == ID)));
}
}
But it doesn't work :(
Can you tell me how I can do this.
Thanks
Here's my error messages
I can't have access to the User property of the HttpContext Class
I think you were passing wrong parameter: To Get UserId you could try:
var principle = HttpContext.User;
string userID = UserManager.GetUserId(principle);
To get context in your custom ValidationAttribute:
var _context = (ApplicationDbContext)ValidationContext.GetService(typeof(ApplicationDbContext));
I think it may help if you could describe your purpose more clearly and share more codes related
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.