Hi nauna,
Since your api returning List<UserModel> you can validate after getting the response.
In code check if GetCustomers() count equals to zero then show the message IP is allow to use this method.
Or you can add a property to the that will be used to set the message and return from the api so that it can be checked while consuming.
Like below example.
[Route("api/CustomerAPI/GetCustomers")]
[HttpPost]
public List<UserModel> GetCustomers()
{
List<UserModel> user = new List<UserModel>();
string ip = ws.GetIpValue();
if (ip == "Invalid")
{
user.Add(new UserModel {message= "IP is allow to use this method" });
}
else
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM users", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
user.Add(new UserModel
{
username = sdr["username"].ToString(),
password = sdr["password"].ToString()
});
}
}
con.Close();
}
}
}
return user;
}
public class UserModel
{
public string username { get; set; }
public string password { get; set; }
public string message { get; set; }
}
Then check in code like below.
if (GetCustomers()[0].message == "IP is allow to use this method")
{
error();
}