WCF client with ssl and username/password authentication - visual-studio

I am trying to connect to service which is accesssible over https and need authentication.
The error I get is:
Test method
TestProject1.UnitTest1.TestReharB2B
threw exception:
System.ServiceModel.Security.MessageSecurityException:
The HTTP request is unauthorized with
client authentication scheme
'Anonymous'. The authentication header
received from the server was 'Basic
realm="Application"'. --->
System.Net.WebException: The remote
server returned an error: (401)
Unauthorized..
It seems to me like that username and password is not sent to service. What am I missing?
The code:
EndpointAddress address = new EndpointAddress(
new Uri("https://84.52.158.151:8443/JavaStore/services/B2BService"),
EndpointIdentity.CreateDnsIdentity("JavaStore"),
new AddressHeaderCollection()
);
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
var client = new ReharB2BService.B2BServicePortTypeClient(binding, address);
client.ChannelFactory.Credentials.UserName.UserName = "dotNet";
client.ChannelFactory.Credentials.UserName.Password = "dotnetpwd";
client.Open();
client.getAllItems();

Your service returns error because there is some misconfiguration between server and client. Your client uses transport security with UserName message credentials. That means that HTTPS is used and message contains SOAP header with user name and password. But your service returns exception because it (probably IIS) expects transport security with Basic credentials. That means HTTPS and HTTP Basic authentication (HTTP header).

Related

For Jmeter Bearer authorization token request, Getting org.apache.http.conn.HttpHostConnectException

I am sending a HTTPS request which needs a Bearer token. I have added a Header Manager to my HTTP request in JMeter and defined NAMEand VALUE in the Header Manager as Authorization and "Bearer xxxxyxyxyxz" respectively.
The request is running for 21 seconds each time and then its failed with below text
org.apache.http.conn.HttpHostConnectException: Connect to abcd.uat.xyz.com:443 [abcd.uat.xyz.com/11.222.250.10] failed: Connection timed out: connect.
Am I sending the bearer token in right way with Header Manager or is there some other way to send this kind of requests?
Note: Its(The same request and Bearer token) working perfectly in Postman and I am getting the correct response as well.
The issue is resolved after adding Proxy Server details to the request. I have added those in Advanced section of HTTP request.
In Postman, the tool using default system proxy settings, hence working without any issues but when it comes to Jmeter we need to add Proxy Server details explicitly.
Based on the exception it seems, you are trying to access HTTP server with HTTPS request.
Make sure to confirm whether "abcd.uat.xyz.com:443" support https or not..If not simply make your request HTTP by updating Protocol field.

RestTemplate postForObject return HttpClientErrorException: 401 null

I am trying to send a post message to authentication server and I am usign postforobject. When I run the program, i am getting "Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 null" error
RestTemplate rest = new RestTemplate();
String responseEntity;
String uri = "http://...";
responseEntity = rest.postForObject(uri, null, String.class);
System.out.println(responseEntity);
Also I tried
responseEntity = rest.postForObject(uri, "", String.class);
Same result, it didn't work.
When I using Simple Rest Client it works
enter image description here
Any idea?
401 null probably means that the server expected some form of authentication but none was presented (i.e. no Authorization header in your request)
The most likely explanation is: the Simple REST Client (re)uses Chrome's cookies. If you previously authenticated against your service using a form login, the Simple Rest Client will reuse the JSESSIONID cookie. To detect that simply press F12 and open the network tab of your debugger. FYI there are other browser plugins, for example "Advanced REST Client", which give you more visibility over what's being sent to the server.
When you send a request using restTemplate, it doesn't have access to the session cookie (which is to be expected). You should either post to URI that allows anonymous access, or provide some form of authentication in the request. If you require additional help please post your security configuration.

Jersey client with multi domain uri

I am using Jersey Client API to talk to REST server.
Client is having problem in connecting to URL with multiple sub-domains.
EX : https://domain1.domain2.abc.com.
This throws 503 service unavailable.
i trusted all the certs and bypassed host name verification.
Even with HttpsUrlConnection API i get the same 503 error.
But when i hit the same URL with CURL that works fine.
I see the Client API works well with https://domain2.optum.com.
Is there any restriction for Jersey Client (HttpsUrlConnection)API with sub domain names in the URL?

JavaPNS via Apache Web Proxy

My push java app is behind a web proxy. I used below code to set proxy:
ProxyManager.setProxy("", "");
After executed, I got:
javapns.communication.exceptions.CommunicationException: Communication exception: java.io.IOException: Unable to tunnel through. Proxy returns "HTTP/1.1 403 Proxy Error"
Please guide me how to solve this issue.
If there is a username and password required for the proxy, then you also need to set the proxyAuthorization of the ProxyManager.
To encode the username and password, you can use the existing encodeProxyAuthorization method of the ProxyManager:
String encodeProxyAuthorization = ProxyManager.encodeProxyAuthorization(username, password);
ProxyManager.setProxyAuthorization(encodeProxyAuthorization);

Mutual Authentication with HttpClient

i am going to present my problem:
My web application submit a form by https to a server that it needs client certificate for set the communication. It works fine, when i submit the form, the https server asks me for a certificate, i select my certificate and the server response me. But now, i want to post to https server from a servlet with HttpClient because i want to manage the response from https server, and here i have problems...
My code to send a post with HttpClient is:
DefaultHttpClient client = new DefaultHttpClient();
// in the truststore i have the https server certificates for establish connection
// from my client to https server
FileInputStream instream = new FileInputStream(new File(TRUESTORE_PATH));
KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
truststore.load(instream, KEY_TRUESTORE_PASS.toCharArray());
// in the keystore i have the client certificate but without private key because,
// in theory, i shouldn't know it
FileInputStream otherinstream = new FileInputStream(new File(KEYSTORE_PATH));
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(otherinstream,KEY_STORE_PASS.toCharArray());
SSLSocketFactory socketFactory = new SocketFactory(keystore,KEY_STORE_PASS,truststore);
Scheme sch = new Scheme("https", 443, socketFactory);
client.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://remote_server_with_client_authentication");
HttpResponse response = client.execute(httpget);
When i execute this code, in an unit test for example, i have response from https server, but it tells me that i am not authorized. I suspect that i can't authenticate with the https server because the keystore has the client certificate but whitout private key, then i am not authorized.
Then, Is there some way to establish communication with the https server with a client certificate selected by the user?
Thanks!

Resources