0

So I am creating a an API with ASP.net MVC Web API. I currently have a model which contains the fields for a user in the database. I have a password field on this model. See below for an example.

public class Account
{
    [Key]
    public Guid UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set;  }
 }

I return this model using JSON when a controller method is called over HTTP. This works fine. My question is, how do I stop the password field being returned alongside with it? Without removing the field altogether.

My initial idea is to create another model class which I use to return the data without the password field, but I'd rather not repeat myself for the sake of one field.

Any suggestions?

2
  • Do not store passwords in plain text. You should replace that with a hash. Commented Jan 13, 2015 at 13:15
  • Thanks @SLaks. I neglated to mention that the password is hashed and salted. Commented Jan 13, 2015 at 13:17

1 Answer 1

3

You should be able to mark these fields with

 [JsonIgnore]
 [XmlIgnore]
 public string Password { get; set;  }

Preventing these fields to be used in either JSON or XML requests.

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

1 Comment

Absolute star. This did the job.

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.