websocket-manager icon indicating copy to clipboard operation
websocket-manager copied to clipboard

How to access User Context

Open ghost opened this issue 7 years ago • 4 comments

Hello,

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

ghost avatar Apr 25 '17 22:04 ghost

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!

radu-matei avatar Apr 26 '17 11:04 radu-matei

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 :/

ghost avatar Apr 26 '17 17:04 ghost

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.

liddellj avatar Oct 16 '17 15:10 liddellj

Could you give us a sample of the ReceiveAsync?

hkosmidi avatar Nov 07 '17 08:11 hkosmidi