httpclient cannot access the api of incus

httpclient cannot access the api of incus. The client trust certificate has been added to the server. The code is as follows

public IActionResult ChangeVm()
{

        string zs = @"H:\projet\2019test\testrunstr\strrunapi\bin\Debug\net5.0\incus-ui.pfx";          

        var handler = new HttpClientHandler
        {
            ClientCertificateOptions = ClientCertificateOption.Manual,
            SslProtocols = SslProtocols.Default
        };
        
        handler.ClientCertificates.Add(new X509Certificate2(zs, "123",X509KeyStorageFlags.UserKeySet));             

        using (var http=new HttpClient(handler))
        {
           
            var val = new { action = "stop"};
            StringContent sc = new StringContent(JsonConvert.SerializeObject(val));
            sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            string path = "https://192.168.193.183:8443/1.0/instances/test/state?project=default";
            var str = http.PutAsync(path, sc).Result.Content;
            return Json(str);
        }
    }

Certificates trusted by the server have been added

root@debian:~# incus config trust list
±-------------------±-------±------------±-------------±---------------------+
| NAME | TYPE | DESCRIPTION | FINGERPRINT | EXPIRY DATE |
±-------------------±-------±------------±-------------±---------------------+
| incus-uiserver.crt | client | | 97ef3368b0d4 | 2027/01/11 20:17 CST |
±-------------------±-------±------------±-------------±---------------------+

This problem has been bothering me for two days. God please help me.

I am new to incus, I hope someone can help me.

Can you try with curl first to make sure things properly at the API level?

curl --cert /path/to/file.crt --key /path/to/file.key -k https://192.168.193.183:8443/1.0/instances/test/state?project=default
root@debian:/home# curl --cert /home/server2.crt --key /home/server2.key -k https://192.168.193.183:8443/1.0/instances/test/state?project=default
{"type":"sync","status":"Success","status_code":200,"operation":"","error_code":0,"error":"","metadata":{"status":"Stopped","status_code":102,"disk":{"root":{"usage":107884544,"total":3221225472}},"memory":{"usage":0,"usage_peak":0,"total":0,"swap_usage":0,"swap_usage_peak":0},"network":null,"pid":0,"processes":0,"cpu":{"usage":0}}}
root@debian:/home#

This verification is normal and there is no problem

Okay, so then the issue is in your code which isn’t in a language I’m familiar with so you’re going to have to do more digging and debugging there :slight_smile:

okay,thank you