Skip to content

Commit

Permalink
Added support for Client Certificates in the .NET client
Browse files Browse the repository at this point in the history
- Incorporated changes as per code review feedback
#1303
  • Loading branch information
abnanda1 committed Mar 29, 2013
1 parent 243b894 commit 7a1c24a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client/Connection.cs
Expand Up @@ -18,6 +18,9 @@
using Microsoft.AspNet.SignalR.Infrastructure;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#if (NET4 || NET45)
using System.Security.Cryptography.X509Certificates;
#endif

namespace Microsoft.AspNet.SignalR.Client
{
Expand Down Expand Up @@ -66,6 +69,10 @@ public class Connection : IConnection
//The json serializer for the connections
private JsonSerializer _jsonSerializer = new JsonSerializer();

#if (NET4 || NET45)
private X509CertificateCollection certCollection = new X509CertificateCollection();
#endif

/// <summary>
/// Occurs when the <see cref="Connection"/> has received data from the server.
/// </summary>
Expand Down Expand Up @@ -554,6 +561,25 @@ public Task Send(object value)
return Send(this.JsonSerializeObject(value));
}

#if (NET4 || NET45)
/// <summary>
/// Adds a client certificate to the request
/// </summary>
/// <param name="certificate">Client Certificate</param>
public void AddClientCertificate(X509Certificate certificate)
{
lock (_stateLock)
{
if (State != ConnectionState.Disconnected)
{
throw new InvalidOperationException(Resources.Error_CertsCanOnlyBeAddedWhenDisconnected);
}

certCollection.Add(certificate);
}
}
#endif

public void Trace(TraceLevels level, string format, params object[] args)
{
lock (_traceLock)
Expand Down Expand Up @@ -673,6 +699,10 @@ void IConnection.PrepareRequest(IRequest request)
}
#endif
request.SetRequestHeaders(Headers);

#if (NET4 || NET45)
request.AddClientCerts(certCollection);
#endif
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Can be called via other clients.")]
Expand Down
15 changes: 15 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client/Http/HttpWebRequestWrapper.cs
Expand Up @@ -4,6 +4,9 @@
using System.Collections.Generic;
using System.Globalization;
using System.Net;
#if (NET4 || NET45)
using System.Security.Cryptography.X509Certificates;
#endif

namespace Microsoft.AspNet.SignalR.Client.Http
{
Expand Down Expand Up @@ -125,5 +128,17 @@ public void SetRequestHeaders(IDictionary<string, string> headers)
}
}
}

#if (NET4 || NET45)
public void AddClientCerts(X509CertificateCollection certificates)
{
if (certificates == null)
{
throw new ArgumentNullException("certificates");
}

_request.ClientCertificates = certificates;
}
#endif
}
}
11 changes: 11 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client/Http/IRequest.cs
Expand Up @@ -3,6 +3,9 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
#if (NET4 || NET45)
using System.Security.Cryptography.X509Certificates;
#endif

namespace Microsoft.AspNet.SignalR.Client.Http
{
Expand Down Expand Up @@ -48,5 +51,13 @@ public interface IRequest
/// </summary>
/// <param name="headers">request headers</param>
void SetRequestHeaders(IDictionary<string, string> headers);

#if (NET4 || NET45)
/// <summary>
/// Sets client certificates
/// </summary>
/// <param name="certificates">client certificates</param>
void AddClientCerts(X509CertificateCollection certificates);
#endif
}
}
6 changes: 6 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client/IConnection.cs
Expand Up @@ -11,6 +11,9 @@
using Microsoft.AspNet.SignalR.Client.Transports;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#if (NET4 || NET45)
using System.Security.Cryptography.X509Certificates;
#endif

namespace Microsoft.AspNet.SignalR.Client
{
Expand Down Expand Up @@ -46,6 +49,9 @@ public interface IConnection
void OnConnectionSlow();
void PrepareRequest(IRequest request);
void UpdateLastKeepAlive();
#if (NET4 || NET45)
void AddClientCertificate(X509Certificate certificate);
#endif
void Trace(TraceLevels level, string format, params object[] args);
}
}
Expand Up @@ -19,7 +19,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;CLIENT_NET4;NET4</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Microsoft.AspNet.SignalR.Client.XML</DocumentationFile>
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System;
using System.Net.WebSockets;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNet.SignalR.Client.Http;

namespace Microsoft.AspNet.SignalR.Client.Transports
Expand Down Expand Up @@ -91,6 +92,16 @@ public void SetRequestHeaders(IDictionary<string, string> headers)
}
}

public void AddClientCerts(X509CertificateCollection certificates)
{
if (certificates == null)
{
throw new ArgumentNullException("certificates");
}

_clientWebSocket.Options.ClientCertificates = certificates;
}

public void Abort()
{

Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.AspNet.SignalR.Hosting.Memory/Request.cs
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNet.SignalR.Owin;
using Microsoft.AspNet.SignalR.Owin.Infrastructure;
using IClientRequest = Microsoft.AspNet.SignalR.Client.Http.IRequest;
Expand Down Expand Up @@ -85,5 +86,9 @@ public void SetRequestHeaders(IDictionary<string, string> headers)
_requestHeaders.SetHeader(headerEntry.Key, headerEntry.Value);
}
}

public void AddClientCerts(X509CertificateCollection certificates)
{
}
}
}

0 comments on commit 7a1c24a

Please sign in to comment.