Restsharp :The SSL connection could not be established - asp.net-core

An app is run on netcore2.1.5,win7.
I use restsharp to request the API and need to take client certificates, always return the error:
The SSL connection could not be established, see inner exception.
Authentication failed, see inner exception
The inner exception content:
System.ComponentModel.Win32Exception (0x80090326): 接收到的消息异常,或格式不正确。
English version:
"The message received is abnormal or not formatted correctly"
I user postman to request the API and take the same crt file and response the
result result.
My code on below:
string clientCertfile = #"E:\https\client.crt";
var client = new RestClient("https://apiserver/iocm/app/sec/v1.1.0/login");
X509Certificate2 certificates = new X509Certificate2(clientCertfile);
//X509Certificate2 certificates = GetMyX509Certificate(clientCertfile);
client.ClientCertificates = new X509Certificate2Collection(){ certificates };
client.RemoteCertificateValidationCallback =
new RemoteCertificateValidationCallback(OnRemoteCertificateValidationCallback);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("appId", "rIsyJsnMtOrKfpSO90");
request.AddParameter("secret", "8v0fH0ztunjP1oXT");
IRestResponse response = client.Execute(request);
postman test image

Related

C# JIRA Error Message: The request was aborted: Could not create SSL/TLS secure channel

When I run this it works and gives a valid reponse
private readonly Lazy<Jira> jiraClient = new Lazy<Jira>(() => Jira.CreateRestClient("https://jira...", "name", "pass"));
but when I try to run this I get an error.
Issue issue = await this.jiraClient.Value.Issues.GetIssueAsync(jiraId);
Error Message: The request was aborted: Could not create SSL/TLS secure channel
here is the answer
The request was aborted: Could not create SSL/TLS secure channel
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Getting Unauthorized error when trying to execute the RestSharp request that retrieves data from ArangoDB

If I send the http request through Postman it works and I get the result. But the same is not working and getting Unauthorized when I execute through RestSharp.
Below is the code snippet:
var client = new RestClient(
"http://Username:Password#localhost:port/_db/databaseName/_api/simple/all");
var request = new RestRequest(Method.PUT);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json",
"{\n \"collection\":\"collectionName\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;
According to the restsharp wiki you cannot specify the authentication parameters via the URL:
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("username", "foo", "password", "bar");
var request = new RestRequest("resource", Method.GET);
client.Execute(request);
in general its a good idea in such a case to drop https, and use wireshark or ngrep to inspect whats going on on the wire:
GET /_db/_system/_api/version?details=true HTTP/1.1
Host: 127.0.0.1
Connection: Keep-Alive
User-Agent: ArangoDB
Accept-Encoding: deflate
Authrization: Basic cm9vdDo=
to inspect the actualy generated authentication headers.
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("admin","admin");
var request = new RestRequest(Method.GET);
client.Execute(request);
This works for me..

HTTPS REST call from salesforce returns error

I am trying to call a end point using following code, but it always returns the below error.
Can you suggest what is the possible error?
HttpRequest req = new HttpRequest();
req.setEndpoint('https://ntrs-com.apibasicauth.btglss.net/bitglassapi/data/v1/soql');
req.setMethod('POST');
//req.setHeader('Authorization', '');
req.setHeader('Content-Type', 'application/json');
req.setBody('{"responseformat":"json","query":"select Id from Account","offset":"1","limit":"10"}');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('---Body----'+res.getBody());
Error says:
Line: 8, Column: 1 System.CalloutException:
java.security.cert.CertificateException: No subject alternative DNS
name matching ntrs-com.apibasicauth.btglss.net found.

RabbitMQ HTTP API request 401 Unauthorized

I'm trying to access to RabbitMQ rest, but I got 401 unauthorized error. I want to access to queue information and to get messages number.
I found this as a solution
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");
HttpPut request = new HttpPut(
"/api/queues/%2F/queue-name");
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("guest", "guest"));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
request.addHeader("Content-Type", "application/json");
StringEntity input = new StringEntity(
"{\"vhost\":\"/\",\"durable\":\"false\",\"auto_delete\":\"false\",\"arguments\":{}}");
request.setEntity(input);
HttpResponse response = httpClient.execute(targetHost, request, localcontext);
but it doesn't work. I saw that DefaultHttpClient class is depreciated so I tried something like this
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");
HttpPut request = new HttpPut("/api/whoami");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CredentialsProvider credentialProvider = new BasicCredentialsProvider();
credentialProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("guest","guest")
);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialProvider);
context.setAuthCache(authCache);
request.addHeader("Content-Type", "application/json");
HttpResponse response = httpClient.execute(targetHost, request, context);
and then to access to REST thought WebTarget, something like this
WebTarget queueREST = RESTClientManager.getClient().target("xx.xx.xx.xx:15672/api/queues/%2F/queue-name");
but I still get error 401 Unauthorized. Any suggestion how to solve this problem?
"guest" user can only connect via localhost. To allow remote connections using guest change rabbitmq.config and add [{rabbit, [{loopback_users, []}]}].
[source: https://www.rabbitmq.com/access-control.html]
In my case, I fix it with:
sudo sed -i 's/{default_pass, <<"guest">>}$/{default_pass, <<"guest">>},\n {loopback_users, []}/' /etc/rabbitmq/rabbitmq.config
I found solution using this https://github.com/rabbitmq/hop .
You could use too my rabbitmq-management-java-client
library which is more complete.
For example a snippet to authenticate and list queues:
RabbitManagementApi api = RabbitManagementApi.newInstance("http://localhost:15672/" , "user" , "password");
List<Queue> queues = api.listQueues("vhost");

Rabbitmq HTTP API request UnAuthorized

I am trying to create a new exchange using the http api request. The URL I have used to create Exchange is , http://guest:guest#localhost:55672/api/exchanges/%2F/myexq1 but it gives me error of 401 Unauthorized. I am using chrome rest client to do this request. What could be the reason? Any help will appreciated.
Have solve the problem in other way. The error is there while using the URL http://guest:guest#localhost:55672/api/exchanges/%2F/myexq1 . But to acheive my goal I have written a small class. Here is the code:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 55672, "http");
HttpPut request = new HttpPut(
"/api/queues/%2F/q1");
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("guest", "guest"));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
request.addHeader("Content-Type", "application/json");
StringEntity input = new StringEntity(
"{\"vhost\":\"/\",\"durable\":\"false\",\"auto_delete\":\"false\",\"arguments\":{}}");
request.setEntity(input);
HttpResponse response = httpClient.execute(targetHost, request, localcontext);
Jar I have included is:
commons-codec-1.4
commons-logging-1.1.1
httpclient-4.1.3
httpclient-cache-4.1.3
httpcore-4.1.4
httpmime-4.1.3