Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorization Header in SignalR 2.0

I am using WebApi and token based authentication for my api controllers.(Authorization :bearer xyzabc..) I now have a signalR hub and would like to authenticate clients by the same token they have on the client side.

How would i do that ? This link shows how to send token through url parameter, but i am not sure how i can use that token and authenticate the user on server side.

like image 755
Koder Avatar asked Dec 18 '13 07:12

Koder


1 Answers

I solved this by passing the token as a parameter of my Hub method instead of header. but i imagine it is possible to do it using headers too (just extracting the token from Context.Headers or something).

Either way, after getting the token in your hub method, just use this code.

    public Task SendMessage(string message, string token)
    {
        var ticket = Startup.OAuthOptions.AccessTokenFormat.Unprotect(token);
        bool isAuth = ticket.Identity.IsAuthenticated;
       //You can retrieve other details like username and userid from ticket
       ...rest of your code..
    }
like image 177
Koder Avatar answered Jan 03 '23 01:01

Koder