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!
ServicePointManagercode?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?