From 50e5827414fddd4b4a74c4e7d226e34b83ae4a80 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Fri, 28 Oct 2016 10:25:38 -0700 Subject: [PATCH] Add HubCallerContext to Hub - Flow the connection id and user of the caller to the Hub --- samples/SocketsSample/EndPoints/HubEndpoint.cs | 6 ++++-- samples/SocketsSample/EndPoints/RpcEndpoint.cs | 14 +++++++------- samples/SocketsSample/Hubs/Hub.cs | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/samples/SocketsSample/EndPoints/HubEndpoint.cs b/samples/SocketsSample/EndPoints/HubEndpoint.cs index 5a7c6ea36c..69b1429c67 100644 --- a/samples/SocketsSample/EndPoints/HubEndpoint.cs +++ b/samples/SocketsSample/EndPoints/HubEndpoint.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Channels; +using Microsoft.AspNetCore.Sockets; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SocketsSample.Hubs; @@ -28,12 +29,13 @@ namespace SocketsSample return new SingleClientProxy(this, connectionId); } - protected override void Initialize(object endpoint) + protected override void Initialize(Connection connection, object endpoint) { var hub = (Hub)endpoint; hub.Clients = this; + hub.Context = new HubCallerContext(connection.ConnectionId, connection.User); - base.Initialize(endpoint); + base.Initialize(connection, endpoint); } protected override void DiscoverEndpoints() diff --git a/samples/SocketsSample/EndPoints/RpcEndpoint.cs b/samples/SocketsSample/EndPoints/RpcEndpoint.cs index 65c1cd6fda..7c8ae5fccf 100644 --- a/samples/SocketsSample/EndPoints/RpcEndpoint.cs +++ b/samples/SocketsSample/EndPoints/RpcEndpoint.cs @@ -16,8 +16,8 @@ namespace SocketsSample { public class RpcEndpoint : EndPoint { - private readonly Dictionary> _callbacks - = new Dictionary>(StringComparer.OrdinalIgnoreCase); + private readonly Dictionary> _callbacks + = new Dictionary>(StringComparer.OrdinalIgnoreCase); private readonly Dictionary _paramTypes = new Dictionary(); private readonly ILogger _logger; @@ -71,10 +71,10 @@ namespace SocketsSample } InvocationResultDescriptor result; - Func callback; + Func callback; if (_callbacks.TryGetValue(invocationDescriptor.Method, out callback)) { - result = callback(invocationDescriptor); + result = callback(connection, invocationDescriptor); } else { @@ -90,7 +90,7 @@ namespace SocketsSample } } - protected virtual void Initialize(object endpoint) + protected virtual void Initialize(Connection connection, object endpoint) { } @@ -113,7 +113,7 @@ namespace SocketsSample _logger.LogDebug("RPC method '{methodName}' is bound", methodName); } - _callbacks[methodName] = invocationDescriptor => + _callbacks[methodName] = (connection, invocationDescriptor) => { var invocationResult = new InvocationResultDescriptor(); invocationResult.Id = invocationDescriptor.Id; @@ -125,7 +125,7 @@ namespace SocketsSample { object value = scope.ServiceProvider.GetService(type) ?? Activator.CreateInstance(type); - Initialize(value); + Initialize(connection, value); try { diff --git a/samples/SocketsSample/Hubs/Hub.cs b/samples/SocketsSample/Hubs/Hub.cs index b43977fd1f..30b3ff220e 100644 --- a/samples/SocketsSample/Hubs/Hub.cs +++ b/samples/SocketsSample/Hubs/Hub.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; +using System.Security.Claims; using System.Threading.Tasks; - namespace SocketsSample.Hubs { public class Hub { public IHubConnectionContext Clients { get; set; } + + public HubCallerContext Context { get; set; } } public interface IHubConnectionContext @@ -26,4 +28,17 @@ namespace SocketsSample.Hubs /// A task that represents when the data has been sent to the client. Task Invoke(string method, params object[] args); } + + public class HubCallerContext + { + public HubCallerContext(string connectionId, ClaimsPrincipal user) + { + ConnectionId = connectionId; + User = user; + } + + public ClaimsPrincipal User { get; } + + public string ConnectionId { get; } + } }