Avatar of soozh
Flag for Sweden

asked on 

Using signalR in MVC 5 ASP.Net application


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.



Avatar of undefined
Last Comment

8/22/2022 - Mon