using System.Data.SqlClient;

namespace InsecureSQLConnection
{
    public class Class1
    {
        public void StringInConstructor()
        {
            SqlConnection conn = new SqlConnection("Encrypt=true");
        }

        public void StringInProperty()
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Encrypt=true";

        }

        public void StringInBuilder()
        {
            SqlConnectionStringBuilder conBuilder = new SqlConnectionStringBuilder();
            conBuilder.Encrypt = true;
            SqlConnection conn = new SqlConnection(conBuilder.ToString());
        }

        public void StringInBuilderProperty()
        {
            SqlConnectionStringBuilder conBuilder = new SqlConnectionStringBuilder();
            conBuilder.Encrypt = true;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = conBuilder.ToString();
        }

        public void StringInInitializer()
        {
            string connectString = "Server=1.2.3.4;Database=Anything;UID=ab;Pwd=cd;Encrypt=false";
            SqlConnectionStringBuilder conBuilder = new SqlConnectionStringBuilder(connectString) { Encrypt = true};
        }
        

        public void TriggerThis()
        {
            // BAD, Encrypt not specified
            SqlConnection conn = new SqlConnection("Server=myServerName\\myInstanceName;Database=myDataBase;User Id=myUsername;"); // $ Alert
        }

        void Test4()
        {
            string connectString =
                "Server=1.2.3.4;Database=Anything;UID=ab;Pwd=cd"; // $ Source
            // BAD, Encrypt not specified
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString); // $ Alert
            var conn = new SqlConnection(builder.ConnectionString);
        }

        void Test5()
        {
            string connectString =
                "Server=1.2.3.4;Database=Anything;UID=ab;Pwd=cd;Encrypt=false"; // $ Source
            // BAD, Encrypt set to false
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString); // $ Alert
            var conn = new SqlConnection(builder.ConnectionString);
        }
    }
}
