using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*

CREATE TABLE Users ([Id] INT IDENTITY PRIMARY KEY, [UserName] VARCHAR(50), [Password] VARCHAR(255));
GO

INSERT INTO Users VALUES('mario', 'malpeczka');
INSERT INTO Users VALUES('aga', 'nosowka');
INSERT INTO Users VALUES('ela', 'zebatka');
GO

 */
namespace SqlInjection
{
    class Program
    {
        static void Main(string[] args)
        {
            string UserNameInput = "' OR 1=1;INSERT INTO Users VALUES('x', 'x');--";
            string PasswordInput = "";
            SqlConnection c = new SqlConnection("Data source=(local);Initial catalog=Test;Integrated Security=SSPI");

            //SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Users WHERE UserName='" + UserNameInput + "' AND Password='" + PasswordInput + "'", c);
            SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Users WHERE UserName=@UserName AND Password=@Password", c);
            cmd.Parameters.AddWithValue("@UserName", UserNameInput);
            cmd.Parameters.AddWithValue("@Password", PasswordInput);

            try
            {
                c.Open();
                var x = cmd.ExecuteScalar();
            }
            finally
            {
                c.Close();
            }
        }
    }
}
