0

I am trying to create a user model with username and password. I dont want the password to be displayed in the api response, but it is required on signup. So [JsonIgnore] wont work. Any suggestions?

public class User {
    [Required]
    public string Username { get; set; }

    [Required]
    public string Password { get; set; }
}
2
  • 1
    Don't send your domain/entity on API response but create DTO with only the properties you need. Commented Aug 14, 2017 at 9:40
  • Are you saying that you are using the same model for two actions — sign up and something else? Commented Aug 14, 2017 at 9:41

1 Answer 1

3

That's why you should separate your actual entity and model. You should create a model / viewmodel and send that as response from your API / MVC controller like

public class UserModel 
{
  public string Username { get; set; }
}

Another piece of advice, never include any data validation in your actual entity, rather you should have those validation [Required] in your model / viewmodel; and should keep the entity away from this clutter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.