title: SignalR AuthorizeHubConnection
body: <p>I want to authenticate users when they connect to the signalr server. The clients are javascript and are cross-domain. I tried using this <a href="http://geekswithblogs.net/shaunxu/archive/2014/05/27/set-context-user-principal-for-customized-authentication-in-signalr.aspx" rel="nofollow">example</a> and the <code>AuthorizeHubMethodInvocation</code> gets called but the <code>AuthorizeHubConnection</code> never gets called.</p>
  
  <p>I created a new class <code>AuthTicketAttribute</code> where i override the two methods,</p>
  
  <pre><code>public class AuthTicketAttribute : AuthorizeAttribute
  {
      public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request)
      {
          return base.AuthorizeHubConnection(hubDescriptor, request);
      }
  
      public override bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod)
      {
          return base.AuthorizeHubMethodInvocation(hubIncomingInvokerContext, appliesToMethod);
      }
  }
  </code></pre>
  
  <p>and then i added it in the signalr map function</p>
  
  <pre><code>var authorizer = new AuthTicketAttribute();
  var module = new AuthorizeModule(authorizer, authorizer);
  </code></pre>
  
  <p>And then i tried debugging the code, only the <code>AuthorizeHubMethodInvocation</code> gets called when i call a method, the <code>AuthorizeHubConnection</code> doesn't get called when i connect. Any idears?</p>
  
  <p>I then though maybe i could move the code setting the <code>server.User</code> in the enviroment from the <code>AuthorizeHubConnection</code> to the <code>AuthorizeHubMethodInvocation</code>, </p>
  
  <pre><code>    public override bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod)
      {
          IList&lt;Claim&gt; claims = new List&lt;Claim&gt;
          {
              new Claim(ClaimTypes.NameIdentifier, "The users username")
          };
          ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, "Basic", ClaimTypes.NameIdentifier, ClaimTypes.Role);
  
          var environment = hubIncomingInvokerContext.Hub.Context.Request.Environment;
          environment["server.User"] = new ClaimsPrincipal(claimsIdentity);
  
          hubIncomingInvokerContext.Hub.Context = new HubCallerContext(new ServerRequest(environment), hubIncomingInvokerContext.Hub.Context.ConnectionId);
  
          return true;
      }
  </code></pre>
  
  <p>but then i get a <code>NullReferenceException</code> when i set the <code>server.User</code> on the environment object, but none of the objects are null.</p>
  
  <pre><code>   at System.Web.HttpContext.SetPrincipalNoDemand(IPrincipal principal, Boolean needToSetNativePrincipal)
     at System.Web.HttpContext.set_User(IPrincipal value)
     at Microsoft.Owin.Host.SystemWeb.OwinCallContext.Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.IPropertySource.SetServerUser(IPrincipal value)
     at Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.set_ServerUser(IPrincipal value)
     at Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.PropertiesTrySetValue(String key, Object value)
     at Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.System.Collections.Generic.IDictionary&lt;System.String,System.Object&gt;.set_Item(String key, Object value)
     at LifeCommunicationServer.AuthTicketAttribute.AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext, Boolean appliesToMethod) in c:\Projects\TeamFoundation\LifeManager\CareManager-Master\LifeCommunicationServer\LifeCommunicationServer\AuthTicketAttribute.cs:line 62
     at Microsoft.AspNet.SignalR.Hubs.AuthorizeModule.&lt;&gt;c__DisplayClass2b.&lt;BuildIncoming&gt;b__26(IHubIncomingInvokerContext context)
     at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.&lt;&gt;c__DisplayClass1.&lt;&lt;BuildIncoming&gt;b__0&gt;d__3.MoveNext()
  </code></pre>