Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide a property in c# code

Tags:

json

c#

asp.net

I have a Business class say User.cs:

  [Serializable]
    public class User
    {

        public int UserID { get; set; }
        public string UserName { get; set; }
        public int Password { get; set; }

    }

In my server side code I am writing the following code to serialize the user object in JSON format:

User user=SomeUserBLClass.GetUser(1);
Response.Write(new JavaScriptSerializer().Serialize(user));

My requirement is to hide the password being sent to client side i.e I don't want the password field to come in json data. Can you help me fixing this?

like image 861
Rocky Singh Avatar asked Feb 03 '26 15:02

Rocky Singh


1 Answers

You could add the [ScriptIgnore] attribute to Password.

[Serializable]
public class User
{

    public int UserID { get; set; }
    public string UserName { get; set; }

    [ScriptIgnore]
    public int Password {get; set;}
}
like image 61
Binary Worrier Avatar answered Feb 05 '26 04:02

Binary Worrier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!