There is an attribute in the model to the property for checking the captcha. The captcha text is stored in the session context.HttpContext.Session.GetString("CaptchaCode"); Trying to do this:

 protected override ValidationResult IsValid(object value, ValidationContext validationContext) { RegisterFizikModel.InputModel inputModel = (RegisterFizikModel.InputModel)validationContext.ObjectInstance; _captcha = validationContext.HttpContext.Session.GetString("CaptchaCode"); if (inputModel.Captcha != _captcha) { return new ValidationResult(GetErrorMessage()); } return ValidationResult.Success; } 

The error turns out:

ValidationContext does not contain HttpContext

How can I get the value of HttpContext.Session.GetString from the session in the property attribute?

  • Are you writing an attribute for validation or what? Not entirely clear - Andrew NOP
  • Yes attribute of validation - Dmitry Valerievich
  • Those. Do you apply it to the class property of a model? - Andrei NOP
  • Yes, to the property of the model. `[Required] [Display (Name =" Enter the number from the image ")] [ValidCaptcha] [StringLength (4)] public string Captcha {get; set; } ` - Dmitry Valerievich
  • one
    IModelValidator your attribute class implement IModelValidator ? - Andrew NOP

1 answer 1

If your class implements the IModelValidator interface, then you must implement the code for validation in the Validate method. This method gets the ModelValidationContext context parameter and from it you can get to the session data:

 context.ActionContext.HttpContext.Session...