【问题标题】:Websocket Android SignalR with Forms Authentication don´t connect带有表单身份验证的 Websocket Android SignalR 不连接
【发布时间】:2015-07-07 22:27:11
【问题描述】:

这是我的场景:

一个启用了 FormsAuthentication 的 SignalR 服务器和一个端点 (WebAPI2),我可以在其中发布身份验证请求以获取身份验证票 (Cookie)。

将向此服务器发出身份验证请求的 Android 应用程序获取身份验证 cookie 并使用该 cookie 连接到 signalR HUB,作为经过身份验证的用户。

使用 ServerSentEventsTransport 时,一切正常,客户端可以验证并连接到服务器,没有问题。

但是,当使用 WebsocketTransport 时,连接不会完成。日志波纹管显示它挂在哪里:

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"}]

有趣的一点是,如果我从web.config 中删除<authentication mode="Forms"/> 配置,连接会成功建立,但没有身份验证数据(如预期的那样)

是否有人可以在需要身份验证和授权(使用 HUB 上的 Authorize 属性)的 HUB 上成功实现 Android SignalR 客户端,使用 Websockets?如果是,怎么做???

谢谢!!

【问题讨论】:

    标签: android asp.net websocket signalr signalr-hub


    【解决方案1】:

    我遇到了同样的问题。我已经使用https://github.com/SignalR/java-client 连接到信号服务器。服务器设置了表单身份验证。 首先,我调用了常规的 api 方法来验证用户并获取 cookie,然后我在每个请求中都发送了该 cookie。 Web 套接字客户端的库本身使用仅带有 uri 参数的构造函数。 WebsocketTransport 类:

     mWebSocketClient = new WebSocketClient(uri);
    

    为了使用 cookie 设置标题,我需要更改 lib 以使用此构造函数:

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

    其中标头包含标头 Cookie。

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 2015-06-02
      • 2021-08-06
      • 2017-07-26
      • 1970-01-01
      • 2015-01-25
      • 2015-05-11
      • 2019-03-15
      • 2014-07-05
      相关资源
      最近更新 更多