33

I am using signalr and asp.net MVC3 to build a sample chat application. Here is what my signalr hub looks like

public class MyHub:Hub,IDisconnect
{
 public Task Join()
 {
  string username = HttpContext.Current.User.Identity.Name;
  //find group based on username
  string group = getGroup(username)
  return Groups.Add(Context.ConnectionId, group);
 }

 public void doStuff()
 {
  string group = getGroup();
  Clients[group].doStuffOnBrowser();
 }
}

My problem is that my app crashed when the page loads. on stepping through with the debugger, I found that HttpContext.Current.User.Identity.Name is null even though the user has already been authenticated. How can I get the username in my Task Join() method?

  • Which authentication mode do you use (forms, windows, etc.)? Are you sure that the user is authenticated? Because in a new intranet mvc3 app both the `HttpContext.Current.User` and `Context.User` aren't null and containing the current user. – nemesv Aug 28 '12 at 13:02
  • @nemesv - My application uses forms authentication and is a internet application. The page that uses signalr can be accessed only if the user is authenticated –  Aug 28 '12 at 19:32
  • Hello @user1, can you provide me a demo for multiple chat rooms using signalR. I want to implement chat server like chat.stackoverflow.com – Mohd Aman Dec 09 '15 at 05:50
  • my email id is: moaman@88gmail.com. if u have any demo please send me. – Mohd Aman Dec 09 '15 at 06:31

1 Answers1

35

When using SignalR hubs you can use the HubCallerContext.User property to:

Gets the user that was part of the initial http request.

So try it with:

public Task Join()
{
    string username = Context.User.Identity.Name;
    //find group based on username
    string group = getGroup(username)
    return Groups.Add(Context.ConnectionId, group);
}
Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • when I type HubCallerContext. the intellisense only shows 2 methods - "Equals" and "ReferenceEquals". I am using the signalr version 0.5.3 –  Aug 26 '12 at 14:09
  • No you don't need to type `HubCallerContext` instead of use the `Context` property of the `Hub` as in my sample: `string username = this.Context.User.Identity.Name;` like you already do with `Context.ConnectionId`. I've updated my answer to make it more clear. – nemesv Aug 26 '12 at 14:10
  • 3
    I tried your code snippet but Context.User.Identity.Name is still null when I step through the debugger. Any thoughts? –  Aug 26 '12 at 16:15
  • @dfowler - yes, I am building a ASP.Net MVC3 application and installed signal R 0.5.3 through nuget –  Aug 28 '12 at 19:31
  • 11
    @user1 make sure that `app.MapSignalR();` in Startup class is after configuring authenticaltion i.e. `ConfigureAuth(app);` Otherwise your Context.User will be null http://stackoverflow.com/a/22028296/2073920 – Abdul Rauf Nov 08 '15 at 16:07
  • 4
    @user1 I know that this question is 3 years old but it is still relevent because I have faced that same `Context.User` null issue yesterday and have solved it. So for saving time of other people, I have commented my solution :) – Abdul Rauf Nov 09 '15 at 04:39
  • 1
    @AbdulRauf Post your comment as an Answer :) – Jalal Feb 07 '19 at 06:48
  • @Jalal I posted it as comment because I think it wasn't a complete answer – Abdul Rauf Feb 07 '19 at 07:18