title: SignalR Mapping user to connectionID with custom authentication
body: <p>I am trying to implement a chat application. Users should be able to send messages to specific users. In order to do that I need to map usernames to their connectionIDs.</p>
  
  <p>My client is using custom authentication. Username is stored in Session["User"]. Therefore I don't have the username stored in Context.User.Identity.Name, which is where SignalR normally takes the username from.</p>
  
  <p>How else can I get the username of the logged in user so that I can map it to Context.ConnectionID?</p>
  
  <p>Here is some sample implementation of public class ChatHub : Hub I found on the web.</p>
  
  <pre><code>private readonly static ConnectionMapping&lt;string&gt; _connections =
          new ConnectionMapping&lt;string&gt;();
  
      public void SendChatMessage(string who, string message)
      {
          string name = Context.User.Identity.Name;
  
          foreach (var connectionId in _connections.GetConnections(who))
          {
              Clients.Client(connectionId).addChatMessage(name + ": " + message);
          }
      }
  
      public override Task OnConnected()
      {
          string name = Context.User.Identity.Name;
  
          _connections.Add(name, Context.ConnectionId);
  
          return base.OnConnected();
      }
  </code></pre>