1

I created a SQL CLR stored procedure that is triggered when an INSERT happens in the database. The stored procedure then uses HttpWebRequest to make an API JSON POST.

The problem is that API uses SSL so when the stored procedure is triggered, I get an error:

Msg 6522, Level 16, State 1, Procedure OnInsert, Line 0 [Batch Start Line 0]
A .NET Framework error occurred during execution of user-defined routine or aggregate "OnInsert":
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

I am using SQL Server 2014 with .NET Framework 4.5, database is set to:

ALTER DATABASE TestDatabase SET TRUSTWORTHY ON;

Assembly is also using .NET Framework 4.5, and the permission state for the assembly used is: EXTERNAL_ACCESS; I tried using UNSAFE but still get the same errors.

Here is my stored procedure:

public readonly static string _url = "https://localhost:44381/api/test";
[Microsoft.SqlServer.Server.SqlProcedure]
public static void OnInsert()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
    request.ContentType = "application/json; charset=utf-8";
    request.Method = "POST";
    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        string json = "{ \"Test\" : \"12345\" }";
        streamWriter.Write(json);
        streamWriter.Flush();
    }

    var httpResponse = (HttpWebResponse)request.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
    }
}

I tried adding:

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

But I still get the same error.

When I added:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => (true);

I get this error:

Msg 6522, Level 16, State 1, Procedure OnInsert, Line 0 [Batch Start Line 0]
A .NET Framework error occurred during execution of user-defined routine or aggregate "OnInsert":
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I tried making the same call via Postman and it was successful. What is bothering me is that I even made a Console Application (.NET Framework 4.5) with the same code as in the SQL CLR stored procedure and it was also successful.

Here is my console app:

    public readonly static string _url = "https://localhost:44381/api/test";

    static void Main(string[] args)
    {
        Create();
    }
    
    public static void Create()
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
        request.ContentType = "application/json; charset=utf-8";
        request.Method = "POST";

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = "{ \"Test\" : \"12345\"}";
            streamWriter.Write(json);
            streamWriter.Flush();
        }

        var httpResponse = (HttpWebResponse)request.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
        }
    }

Thanks in advance!

14
  • Exactly where did you try adding the ServicePointManager code? Commented Feb 23, 2021 at 21:15
  • Before HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url); Why? Commented Feb 23, 2021 at 21:19
  • Is the assembly signed? Commented Feb 23, 2021 at 21:21
  • Nope, that's why I am using ALTER DATABASE TestDatabase SET TRUSTWORTHY ON; I wanted to make sure everything is functioning and sign the assembly later since I can set permissions to EXTERNAL_ACCESS and UNSAFE with TRUSTWORTHY on. You think that signing the assembly could solve the problem? Commented Feb 23, 2021 at 21:26
  • Honestly I'm not sure. Thinking back to when I once did this I think I signed the assembly. Commented Feb 23, 2021 at 21:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.