cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
750
Views
0
Helpful
3
Replies

API Authentification

gerry.sheehan1
Level 1
Level 1

I'm attempting to set up a connection to Stealthwatch via an SSIS c# script. The following fails on the last line with message "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." The curl command in the documentation has a "-K" option which might be the issue, but I don't know how that needs to be reflected in my json request.

 

string wUrl = "https://stealthwatch.nas.hscn.nhs.uk/token/v2/authenticate";  

var httpRequest = (HttpWebRequest)WebRequest.Create(wUrl); 

httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
var data = "{\"username\":\"gsheehan\",\"password\":\"****\"}";

using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(data);
}

var httpResponse = (HttpWebResponse)httpRequest.GetResponse();

1 Accepted Solution

Accepted Solutions

Looks like I had an issue with how I passed in the credentials. The following worked:

 

// Build Username/password in to POST data
var postData = "username=" + Uri.EscapeDataString(username);
postData += "&password=" + Uri.EscapeDataString(password);
var data = Encoding.ASCII.GetBytes(postData);

httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = data.Length;
using (var streamWriter = httpRequest.GetRequestStream())
{
streamWriter.Write(data, 0, data.Length);
}

 

Your help was much appreciated. I'll close the case

View solution in original post

3 Replies 3

Marcel Zehnder
Spotlight
Spotlight

You need to bypass the certificate check (or ignore any ssl related errors). not too sure about C# but according to google this should work:

string wUrl = "https://stealthwatch.nas.hscn.nhs.uk/token/v2/authenticate";  
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };   
var httpRequest = (HttpWebRequest)WebRequest.Create(wUrl); 

httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
var data = "{\"username\":\"gsheehan\",\"password\":\"****\"}";

using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(data);
}

var httpResponse = (HttpWebResponse)httpRequest.GetResponse();

HTH
Marcel

 

Thanks Marcel. It made a difference, in that I receive a different error now : "The remote server returned an error: (415) Unsupported Media Type."

I added extra code:

    httpRequest.MediaType = "application/json";
    httpRequest.Accept = "application/json";

but it hasn't made any difference.

Looks like I had an issue with how I passed in the credentials. The following worked:

 

// Build Username/password in to POST data
var postData = "username=" + Uri.EscapeDataString(username);
postData += "&password=" + Uri.EscapeDataString(password);
var data = Encoding.ASCII.GetBytes(postData);

httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = data.Length;
using (var streamWriter = httpRequest.GetRequestStream())
{
streamWriter.Write(data, 0, data.Length);
}

 

Your help was much appreciated. I'll close the case