Perfect! This stack trace gives us the exact root cause. The error is occurring in Interop.OpenSsl.AllocateSslContext, which means your .NET runtime is trying to use OpenSSL but failing to initialize it properly.
This is a platform-specific OpenSSL issue. Let me provide you with several solutions, starting with the most likely to work:
Solution 1: Use HttpClientHandler Instead (Immediate Fix)
Since SocketsHttpHandler relies on OpenSSL and that's failing, let's fall back to the platform's native SSL implementation by using HttpClientHandler:
using System;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class OdooAuth
{
static readonly string odooUrl = "https://odoo.local";
static readonly string db = "odoodb";
static readonly string username = "admin";
static readonly string password = "admin";
public static async Task TestAsync()
{
try
{
// Use HttpClientHandler instead of SocketsHttpHandler
// This uses the platform's native SSL (SChannel on Windows, SecureTransport on macOS)
var handler = new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};
using (var client = new HttpClient(handler))
{
var loginPayload = new
{
jsonrpc = "2.0",
method = "call",
@params = new
{
db,
login = username,
password
}
};
var loginResponse = await client.PostAsync(
$"{odooUrl}/web/session/authenticate",
new StringContent(JsonSerializer.Serialize(loginPayload), Encoding.UTF8, "application/json")
);
loginResponse.EnsureSuccessStatusCode();
var loginJson = JsonDocument.Parse(await loginResponse.Content.ReadAsStringAsync());
if (loginJson.RootElement.TryGetProperty("error", out JsonElement error))
{
Console.WriteLine($"Odoo login failed: {error}");
return;
}
if (!loginJson.RootElement.TryGetProperty("result", out JsonElement result) || !result.TryGetProperty("uid", out JsonElement uid))
{
Console.WriteLine("Login failed. Could not find UID in the response.");
Console.WriteLine($"Full Response: {await loginResponse.Content.ReadAsStringAsync()}");
return;
}
Console.WriteLine($"Login successful. UID: {uid.GetInt32()}");
}
}
catch (HttpRequestException ex)
{
Console.WriteLine($"An error occurred while making the request: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
if (ex.InnerException.InnerException != null)
{
Console.WriteLine($"Deeper inner exception: {ex.InnerException.InnerException.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
}
}
}
public class Program
{
public static async Task Main(string[] args)
{
await OdooAuth.TestAsync();
}
}
Solution 2: Fix the OpenSSL Issue (If You Want to Keep Using SocketsHttpHandler)
If you prefer to fix the underlying OpenSSL problem, here are the steps based on your platform:
For Linux (Ubuntu/Debian):
# Install OpenSSL development libraries
sudo apt-get update
sudo apt-get install libssl-dev libssl1.1
# For newer Ubuntu versions (22.04+), you might need:
sudo apt-get install libssl-dev libssl3
# Also ensure ca-certificates are up to date
sudo apt-get install ca-certificates
sudo update-ca-certificates
For Linux (CentOS/RHEL/Fedora):
# Install OpenSSL development libraries
sudo yum install openssl-devel
# or for newer versions:
sudo dnf install openssl-devel
For macOS:
# Install OpenSSL via Homebrew
brew install openssl
# You might need to link it properly
brew link openssl --force
# Set environment variables (add to your ~/.zshrc or ~/.bash_profile)
export DYLD_LIBRARY_PATH="/usr/local/opt/openssl/lib:$DYLD_LIBRARY_PATH"
export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig:$PKG_CONFIG_PATH"
For Windows:
This error is less common on Windows, but if you're using WSL or a container:
# In WSL, install OpenSSL
sudo apt-get install libssl-dev
Solution 3: Alternative HTTP Client Configuration
If both approaches above fail, you can try this alternative configuration that forces the use of a specific SSL provider:
public static async Task TestAsync()
{
try
{
// Alternative: Set environment variable to force specific SSL provider
Environment.SetEnvironmentVariable("DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER", "0");
var handler = new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};
using (var client = new HttpClient(handler))
{
// ... rest of your code remains the same
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
Recommendation
Start with Solution 1 (using HttpClientHandler). This should work immediately and bypass the OpenSSL initialization issue entirely. The HttpClientHandler uses your operating system's native SSL implementation instead of trying to load OpenSSL libraries.
If Solution 1 works, you can stick with it for your development needs. If you specifically need SocketsHttpHandler for some reason (like advanced networking features), then proceed with Solution 2 to fix the OpenSSL installation.
Try Solution 1 first and let me know if it resolves the issue!