radu-matei/websocket-manager

How to access User Context

Opened this issue · 4 comments

Hello,

I would like to access my User Identity from any Handler, any ideas to do that?

What type of authentication are you using? Cookie authentication, bearer token?

I haven't had a chance to take a look at this, but any help would be appreciated. If I get any time I will take a look at this, it is among the priorities for this.

Thanks!

I use Cookie authentication.

My current workaround is to create a SuperSocket class:
`
public class SuperSocket
{
public WebSocket Socket
{
get;
set;
}
public ClaimsPrincipal User
{
get;
set;
}

    public SuperSocket(WebSocket socket, ClaimsPrincipal user)
    {
        Socket = socket;
        User = user;
    }
}

`

and in the Invoke method of the WebSocketManagerMiddleware class I instantiate my SuperSocket like that:
var superSocket = new SuperSocket(socket, context.User);

But I'm not sure if it's a good idea to proceed like that :/

I just encountered the same requirement. My approach was to modify WebSocketHandler.ReceiveAsync to allow passing of the HttpContext, which allows access to a ClaimsPrincipal via HttpContext.User.

Could you give us a sample of the ReceiveAsync?