Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

ModelState.IsValid reports valid model while it is null #4750

@janpieterz

Description

@janpieterz

Possibly a dupe of #4607 but since it seemed to be (somehow) resolved there, I thought it smart to file another issue to validate if there is a difference.

Relevant bits:

public async Task<IActionResult> Login([FromBody]LoginViewModel model) { if (ModelState.IsValid) { var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, true, lockoutOnFailure: false); if (result.Succeeded) { _logger.LogInformation(1, "User logged in."); return Json(User.Identity.Name); } else { return BadRequest("Invalid login attempt."); } } else if (HttpContext.User.Identity.IsAuthenticated) { return Json(User.Identity.Name); } return BadRequest("Cannot login."); }
public class LoginViewModel { [Required] [EmailAddress] public string Email { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } }
services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.Converters.Add(new VersionConverter()); });

When calling the method without any parameters in the body, the model rightfully becomes null. I can make this a manual check of course, but it seems if there is a model state, that has parameters that are null but contain properties with the required attribute, that the model state shouldn't succeed.

Thoughts? Easy to circumvent with an extra null check, but it feels a bit counter intuitive (maybe I'm missing something though!)

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions