Skip to content

Commit 8b79c44

Browse files
committed
feat(SSL): Allow self signed certificates
1 parent 700f4d2 commit 8b79c44

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ void Example()
144144
}
145145
```
146146

147+
## Self signed certificates
148+
You can disable SSL verification on calls to Watson (only do this if you really mean to!).
149+
```cs
150+
void Example()
151+
{
152+
AssistantService assistant = new AssistantService("<username>", "<password>", "<version-date>");
153+
assistant.SendAsInsecure(true);
154+
var results = assistant.Message("<workspace-id>", "<message-request>");
155+
}
156+
```
157+
147158
## Documentation
148159
Click [here][dotnet-standard-sdk-documentation] for documentation by release and branch.
149160

src/IBM.WatsonDeveloperCloud/Http/IClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@ public interface IClient : IDisposable
5050
IRequest SendAsync(HttpMethod method, string resource);
5151

5252
IRequest SendAsync(HttpRequestMessage message);
53+
54+
void SendAsInsecure(bool insecure);
5355
}
5456
}

src/IBM.WatsonDeveloperCloud/Http/WatsonHttpClient.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ public class WatsonHttpClient : IClient
3535

3636
public MediaTypeFormatterCollection Formatters { get; protected set; }
3737

38+
public bool Insecure = false;
39+
3840
public WatsonHttpClient(string baseUri)
3941
{
40-
this.BaseClient = new HttpClient();
42+
if (Insecure)
43+
{
44+
var httpClientHandler = new HttpClientHandler();
45+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
46+
this.BaseClient = new HttpClient(httpClientHandler);
47+
}
48+
else
49+
{
50+
this.BaseClient = new HttpClient();
51+
}
4152
this.Filters = new List<IHttpFilter> { new ErrorFilter() };
4253
if (baseUri != null)
4354
this.BaseClient.BaseAddress = new Uri(baseUri);
@@ -47,21 +58,35 @@ public WatsonHttpClient(string baseUri)
4758

4859
public WatsonHttpClient(string baseUri, string userName, string password)
4960
{
50-
this.BaseClient = new HttpClient();
51-
61+
if (Insecure)
62+
{
63+
var httpClientHandler = new HttpClientHandler();
64+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
65+
this.BaseClient = new HttpClient(httpClientHandler);
66+
}
67+
else
68+
{
69+
this.BaseClient = new HttpClient();
70+
}
5271
this.Filters = new List<IHttpFilter> { new ErrorFilter() };
53-
5472
if (baseUri != null)
5573
this.BaseClient.BaseAddress = new Uri(baseUri);
56-
5774
this.Formatters = new MediaTypeFormatterCollection();
58-
5975
this.WithAuthentication(userName, password);
6076
}
6177

6278
public WatsonHttpClient(string baseUri, string userName, string password, HttpClient client)
6379
{
64-
this.BaseClient = client;
80+
if (Insecure)
81+
{
82+
var httpClientHandler = new HttpClientHandler();
83+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
84+
this.BaseClient = new HttpClient(httpClientHandler);
85+
}
86+
else
87+
{
88+
this.BaseClient = new HttpClient();
89+
}
6590
this.Filters = new List<IHttpFilter> { new ErrorFilter() };
6691
if (baseUri != null)
6792
this.BaseClient.BaseAddress = new Uri(baseUri);
@@ -165,5 +190,10 @@ protected virtual void Dispose(bool isDisposing)
165190
{
166191
Dispose(false);
167192
}
193+
194+
public void SendAsInsecure(bool insecure)
195+
{
196+
Insecure = insecure;
197+
}
168198
}
169199
}

src/IBM.WatsonDeveloperCloud/Service/WatsonService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,10 @@ public void SetEndpoint(string url)
140140
_userSetEndpoint = true;
141141
this.Endpoint = url;
142142
}
143+
144+
public void SendAsInsecure(bool insecure)
145+
{
146+
this.Client.SendAsInsecure(insecure);
147+
}
143148
}
144149
}

0 commit comments

Comments
 (0)