- This topic has 2 replies, 3 voices, and was last updated January 12, 2022 by David L.
Rest API and C# – Authentication
The forum ‘Scripting and APIs’ is closed to new topics and replies.
Hello, I’m hoping to utilize an existing c# application to read some alert info from our on-prem Zerto installation. I see that you can pass authentication info using PowerShell, but I don’t see how to using the Rest API. Typically for Rest I’d use RestSharp.dll and Newtonsoft.Json.dll.
The returns are null which indicates to me that authentication is required, which I’m not sending because I’m not sure how to. Anyone doing this? Hoping not to have to use powershell to get this to work.
Thanks!
Joe
I would love to know the answer to this question as well.
Here’s one way to do it.
// Build authentication string
byte[] bytes = Encoding.UTF8.GetBytes($"{_options.Username}:{_options.Password}");
string authorizationString = Convert.ToBase64String(bytes);
// Add headers to the HTTP client
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorizationString);
// Build HTTP message and send
string url = serviceLocation + "session/add";
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, url);
var sessionResponse = await client.SendAsync(message);
// Pull out the session ID from the response header
string zertoSession = sessionResponse.Headers.GetValues("x-zerto-session").FirstOrDefault();
Once you have that session ID, you add it as a header to the HttpClient object for subsequent requests:
client.DefaultRequestHeaders.Add("x-zerto-session", zertoSession);
The forum ‘Scripting and APIs’ is closed to new topics and replies.