locked
Mapping unathenticated users to connections RRS feed

  • Question

  • User962630291 posted

    Hello all,

    I'm trying to map unathenticated users to connections. I am writing a chat application and I want the connection to persist between page loads and new tabs, so my idea is to create a guid in the form of a cookie on the client's browser. This cookie (the idea) would then become the connection id I can identify a user to. 

    I've written up most of the details on Stack Overflow (http://stackoverflow.com/questions/40623642/signalr-mapping-unathenticated-users-to-custom-id), but in short I had two questions

    1. How should my custom IUserIdProvider look if the user doesn't have a "guid" cookie set? Where does this fit into the pipeline if a user comes in with no "guid" cookie

    public class MyUserIdProvider : IUserIdProvider
        {
            public string GetUserId(IRequest request)
            {
                try
                {
                    if (request.Cookies["guid"] != null)
                    {
                        return request.Cookies["guid"].Value;
                    }
                }
                catch (KeyNotFoundException kex)
                {
                    // what to return here?
                    return Guid.NewGuid().ToString();
                }
                // what to return here?
                return "";                       
            }
        }

    2. How do I reference this new "id" (guid) in my hub class? I had been previously using Context.ConnectionId

    // Join chat room
            public Task JoinRoom(string room)
            {
                return Groups.Add(Context.ConnectionId, room);
            }

    I'll be happy to clarify anything or elaborate further, thank you for your time

     

    Wednesday, November 16, 2016 3:48 AM

All replies

  • User283571144 posted

    Hi Zacho7,

    Zacho7

    1. How should my custom IUserIdProvider look if the user doesn't have a "guid" cookie set? Where does this fit into the pipeline if a user comes in with no "guid" cookie

    According to your description, I suggest you could return a null value string to the user.

    We need firstly check the user is IsAuthenticated.

    If user is IsAuthenticated, then you could return user name.

    Then check the request has cookie.

    If the request has cookie then return the cookie value.

    If the request doesn't have the cookie return "Null Cookie User" string.

    I suggest you could add the if condition in the hub's OnConnected event to check the  Context.RequestCookies[""]'s value.

    If the value is "Null Cookie User", then you could redirect to login page or call another page to add the cookie.

    Zacho7

    2. How do I reference this new "id" (guid) in my hub class? I had been previously using Context.ConnectionId

    You could use Context.RequestCookies[""].value to get the current user's cookie.

    If user authenticated, you could use Context.User.Identity.Name to get the current user's name.

    Best Regards,

    Brando

    Thursday, November 17, 2016 12:57 PM