troubleshooting Question

Using signalR in MVC 5 ASP.Net application

Avatar of soozh
soozhFlag for Sweden asked on
2 Comments1 Solution12 ViewsLast Modified:

I am trying to implement a chat function in an MVC 5 ASP.Net application using signalR.
The application uses the following after the user has been authenticated.  I have to admit I have inherited this code and don't understand really what is happening.  But it works!

     var claims = new List<Claim>();
                                    claims.Add(new Claim(ClaimTypes.NameIdentifier, profile.Id.ToString()));
                                    claims.Add(new Claim(ClaimTypes.Name, loggedIn.Username));
                                    claims.Add(new Claim(ClaimTypes.Email, loggedIn.EmailAddress));
                                    claims.Add(new Claim(ClaimTypes.Role, "User"));  //TODO Add role
                                    claims.Add(new Claim(type: "LoginId", value: loggedIn.Id.ToString()));

                                    var identity = new ClaimsIdentity(claims, "ApplicationCookie");
                                    var context = Request.GetOwinContext();
                                    var authManager = context.Authentication;

                                    authManager.SignIn(new AuthenticationProperties
                                    { IsPersistent = login.RememberMe }, identity);

Open in new window

I have created a signalR hub in my application and I want to stop the client from connecting until they are authenticated, when the client connects I want to know who she/he is.

So my questions are:
1. How do implement the necessary code to ensure that the connection occurs after authentication?
2. How do I get the user identity in the hub code? I have overridden the onConnected method so i can hold a dictionary of users.  But I need the user identity as well as the connectionId.


Alfredo Luis Torres Serrano
Senior Net Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros