Gelöst: API via C# erste Schritte

Started by LHBL2003, April 12, 2022, 06:53:50 PM

Previous topic - Next topic
April 12, 2022, 06:53:50 PM Last Edit: April 12, 2022, 07:34:14 PM by LHBL2003
Ich versuche gerade auf die API via C# zuzugreifen.
Allerdings scheitere ich etwas an der Authentifizierung.

Basis war zunächst diese Doku:
https://docs.opnsense.org/development/how-tos/api.html

Zunächst habe ich erst einmal die SSL Zertifikatsprüfung in meiner Program.cs unterdrückt:



        using System.Net;

        ...

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           

            // SSL-Validierungsprüfung global für die gesamte Anwendung umgehen.
            // https://stackoverflow.com/questions/10397736/restsharp-ignore-ssl-errors
            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

            Application.Run(new Form1());
        }



Nun hänge ich irgendwie an der Autentifizierung fest.


using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace FE_FirewallInternetConfigurator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Test_Click(object sender, EventArgs e)
        {
            // SSL-Validierungsprüfung wird in der Program.cs umgangen
            var client = new RestClient(@"https://MEINEFIREWALL:8443/");
           
            string consumerKey = "MEIN KEY";
            string consumerSecret = "MEIN SECRET";
            client.Authenticator = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret);

            //client.Authenticator = new HttpBasicAuthenticator(@"UserName", @"Passwort");

            var request = new RestRequest(@"api/core/firmware/status");

            var queryResult = client.ExecuteGetAsync<Object>(request).Result;
            string json = JsonConvert.SerializeObject(queryResult);
            //System.IO.File.WriteAllText(@"C:\...\path.json", json);
        }
    }
}


egal ob über Key/Secret oder Benutzer/PW irgendwie komm ich nicht voran.

"Content":"{\"status\":401,\"message\":\"Authentication Failed

Hat irgend jemand einen funktionierenden Ansatz für mich?
Ist leider auch mein erster API versuch.

Vielen Dank

April 12, 2022, 07:28:51 PM #1 Last Edit: April 12, 2022, 07:33:51 PM by LHBL2003
Die Doku hat geholfen:

https://docs.opnsense.org/development/api.html

Der Key und Secret muss als HttpBasicAuthenticator genutzt werden.

client.Authenticator = new HttpBasicAuthenticator(consumerKey, consumerSecret);

So soll es scheinbar beim Postman funktionieren. (Erst mal Postman herunterladen. Das hatte ich schon mal für irgend eine API vor langer Zeit benutzt. :-)


Aktueller Stand:

Program.cs

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           
            // SSL-Validierungsprüfung global für die gesamte Anwendung umgehen.
            // https://stackoverflow.com/questions/10397736/restsharp-ignore-ssl-errors
            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

            Application.Run(new Form1());
        }


Form.cs


        private void Test_Click(object sender, EventArgs e)
        {

            // SSL-Validierungsprüfung wird in der Program.cs umgangen
            var client = new RestClient(@"https://FirewallNameOderIP:8443/");
           
            string consumerKey = "Mein Key";
            string consumerSecret = "Mein Secret";

            client.Authenticator = new HttpBasicAuthenticator(consumerKey, consumerSecret);

            var request = new RestRequest(@"api/core/firmware/status", Method.Get);
           
            var queryResult = client.ExecuteGetAsync<Object>(request).Result;
            string json = JsonConvert.SerializeObject(queryResult);
            //System.IO.File.WriteAllText(@"C:\...\path.json", json);
        }