I am trying to access the current user (i.e. ClaimsPrincipal from identity) in a custom ValidationAttribute, and I haven't figured out how I could do that.
public class UniqueTitleValidator : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// var user = ?
}
}
I know that I could access the user from a HttpContext (but I don't know how to get to the latter).
The main problem is that I don't have access to the User. When I use a custom ValidatorAttribute on a property, when the property gets constructed the User (or also HttpContext) is null. Meaning that I can't e.g. give the User to the Validator's constructor. That's why I'm wondering how the Validator could be made to know something about the User (and/or its claims). If I can access any of e.g. HttpContext or User inside the validator, I can get to all the other runtime information as well. Is my question understandable?
sub
will give u the user id if u just want userId. – Amazonite