0

Can't match the C# code to the Unix command CURL is correct, responce is OK, but C# is {"data":null}:

curl -k -d "username=test@pve&password=User11" https://ip:8006/api2/json/access/ticket

C#

string url = "https://ip:8006/api2/json/access/ticket";
WebRequest myReq = WebRequest.Create(url);
string username = "test@pve";
string password = "User11";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Console.WriteLine(content);
Console.ReadLine();

Response:

{"data":null}

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mikhail Zhuikov
  • 1,213
  • 2
  • 9
  • 19

1 Answers1

0

Thanks Peter Csala, helped a lot, I used RestSharp

  static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
            Run();
        }

        public static void Run() {
            var client = new RestClient("https://ip:8006/api2/json/access/ticket");

            var request = new RestRequest(Method.POST);

            request.AddParameter("username", "test@pve");
            request.AddParameter("password", "password");
            
            IRestResponse response = client.Execute(request);
            var settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Include
            };

            dynamic stuff = JsonConvert.DeserializeObject(response.Content, settings);

            var array = stuff.data;

            string ticket = array.ticket.ToString();
            string CSRFPreventionToken = array.CSRFPreventionToken.ToString();

            //Call Nodes, VM,

            var clients = new RestClient("https://ip:8006/api2/json/nodes/pve01/qemu");

            client.Timeout = 10000;
            
            var requests = new RestRequest(Method.GET);
            requests.AddCookie("PVEAuthCookie", ticket);
            IRestResponse responses = clients.Execute(requests);

            var setting = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };

            dynamic stuffs = JsonConvert.DeserializeObject(responses.Content, setting);

            Console.WriteLine(stuffs);

            Console.ReadLine();
        }
Mikhail Zhuikov
  • 1,213
  • 2
  • 9
  • 19
  • could you please include the links what I have mentioned in the comments? Then we can delete most of the comments for your question to keep the topic clean. – Peter Csala Feb 10 '21 at 12:00