I have codes below on asp net currently detected as a potential XSS attacks during code scanning.
public static UserDTO GetUserByUserId(int userId){
UserDTO user = service.GetUser(userId);
return user;
}
The method sends unvalidated data to a web browser on line 826, which
can result in the browser executing malicious code.
I did try to add HttpUtility.HtmlEncode() for string values but now it gives same error for nullable properties.
public static UserDTO GetUserByUserId(int userId){
UserDTO user = service.GetUser(userId);
UserDTO newuser = new UserDTO();
newuser.id=user.id; //int id
newuser.name = HttpUtility.HtmlEncode(user.name); //name string
newuser.age = user.age; //age int? >>> this line causes xss
return newuser;
}
How do I apply for the XSS prevention in this case for C#?