Websocket Android SignalR with Forms Authentication don´t connect

1.5k views Asked by At

This is my cenario:

A SignalR server with FormsAuthentication enabled and a endpoint (WebAPI2) to where I can post an authentication request to get the Authentication Ticket (Cookie).

An Android application that will make an authentication request to this server, gets the authentication cookie and use that cookie to connect to the signalR HUB, as a authenticated user.

When using ServerSentEventsTransport, everything works fine and the client can authenticate and connect to the server, without problems.

But, when using WebsocketTransport, the connection don´t completes. The log bellows shows where does it hangs:

HubConnection - Creating hub proxy: messagehub
HubProxy MessageHub - Subscribe to event messageReceived
HubProxy MessageHub - Creating new subscription for: messagereceived
HubConnection - Entered startLock in start
: null
HubConnection - Start the connection, using AutomaticTransport transport
HubConnection - Start negotiation
: null
AutomaticTransport - Start the negotiation with the server
HubConnection - Getting connection data: [{"name":"messagehub"}]
HubConnection - Getting connection data: [{"name":"messagehub"}]
AutomaticTransport - Execute the request
Create new thread for HTTP Connection
Execute the HTTP Request
URL: http://www.voicenap.com/signalr/negotiate?clientProtocol=1.3&connectionData=%5B%7B%22name%22%3A%22messagehub%22%7D%5D&
VERB: GET
Header User-Agent: SignalR (lang=Java; os=windows 7; version=2.0)
CONTENT: null
Request executed
AutomaticTransport - Response received
AutomaticTransport - Read response data to the end
AutomaticTransport - Trigger onSuccess with negotiation data: {"Url":"/signalr","ConnectionToken":"gJYpQm9SxUDpk9mzxZkpn5VrojtfHdX4wiJovcDHR7yYttg4DVgpy9EU9JDNOKjndRhctwfCHvg7HFaTOAUZ5tdpi7wL8sq4me6TgRycPrvjxb3CfyEYMHK3nDQY/y7e","ConnectionId":"2c97c4e9-28dc-4ac0-9859-b73c6536d01b","KeepAliveTimeout":20.0,"DisconnectTimeout":30.0,"ConnectionTimeout":110.0,"TryWebSockets":true,"ProtocolVersion":"1.3","TransportConnectTimeout":5.0,"LongPollDelay":0.0}

HubConnection - Negotiation completed
HubConnection - ConnectionId: 2c97c4e9-28dc-4ac0-9859-b73c6536d01b
HubConnection - ConnectionToken: gJYpQm9SxUDpk9mzxZkpn5VrojtfHdX4wiJovcDHR7yYttg4DVgpy9EU9JDNOKjndRhctwfCHvg7HFaTOAUZ5tdpi7wL8sq4me6TgRycPrvjxb3CfyEYMHK3nDQY/y7e
HubConnection - Keep alive timeout: 20.0
HubConnection - Entered startLock in startTransport
HubConnection - Starting the transport
HubConnection - Starting transport for InitialConnection
HubConnection - Getting connection data: [{"name":"messagehub"}]
HubConnection - Getting connection data: [{"name":"messagehub"}]

That interesting point is that, if I remove the <authentication mode="Forms"/> configuration from my web.config, the connection establishes successfully but, without authentication data (as expected)

Does anybody could ever successfully implement an Android SignalR Client, using Websockets, on a HUB that requires authentication and authorization (using the Authorize attribut on the HUB)? If yes, how???

Thanks!!

1

There are 1 answers

0
Oggie On

I had the same problem. I have used the https://github.com/SignalR/java-client to connect to signalr server. The server has the forms authentication set up. First, i have called the regular api method to authenticate user and get the cookie, then I've sent that cookie with every request. The library itself for web socket client uses the constructor with only uri param. Class WebsocketTransport:

 mWebSocketClient = new WebSocketClient(uri);

In order to set header with cookie, I needed to change the lib to use this constructor:

new WebSocketClient(uri,  new Draft_17(), headers, 0){...

where headers contain header Cookie.