Reputation: 133
I have a Posts
model class that has a relation with Users
model.
When I call API to get Posts
with the owner of it (user), it returns with all user info including password.
How to prevent model from returning sensitive information like passwords?
Upvotes: 1
Views: 129
Reputation: 36605
You can use [JsonIgnore]
to avoid serializing the property value:
public class Users
{
public int Id { get; set; }
[System.Text.Json.Serialization.JsonIgnore]
public string Password{ get; set; }
//...
}
Upvotes: 0
Reputation: 3486
You should create new classes that you return from your actions. Only include the fields/information you want to return. These classes are also known as Data Transfer Objects (DTO).
Upvotes: 1