Authentication for ServiceStack JavaScript Server Events Client

Nicolas I

I am trying to setup servicestack with ServerEvents. I have added the plugin for ServerEventsFeature. I am using the Javascript server events client and I tried a simple example by doing this on the client after the user was authenticated.

  var source = new EventSource('http://localhost/ecmapi/event-stream?channel=MyChannel');
  $(source).handleServerEvents({
           handlers: {
                  onConnect: function (subscription) {
                       console.log("connected! welcome " + subscription.displayName);
                  }, 
                  onHeartbeat: function (msg, e) {
                      //console.log("onHeartbeat", msg, e);
                       },
                  showNotification: function (type, e) {
                        console.log("showNotification", type);
                        } 
                  }
           });

On the apphost file:

            Plugins.Add(new ServerEventsFeature
                   {
                       StreamPath = "/event-stream",           
                       HeartbeatPath = "/event-heartbeat",     
                       UnRegisterPath = "/event-unregister",    
                       SubscribersPath = null, 
                       LimitToAuthenticatedUsers = false, 
                       NotifyChannelOfSubscriptions = false
                   });

To send an event to client I am calling:

ServerEvents.NotifyChannel("MyChannel", "cmd.showNotification", message);

And I successfully receive it on the showNotification function on the client.

However, the subscription.displayName is auto generated and is not related to the currently logged in user. Also, if I set the LimitToAuthenticatedUsers to true then a 401 error is retruned.

My question is, what else is needed so that the javascript server events client can work with the user that is already logged on? I will want to use the ServerEvents.NotifyUserName or ServerEvents.NotifyUserId function instead and also only allow authenticated users to subscribe.

Thanks!

** The request and response headers for auth and EventSource as requested **

Request auth

POST http://localhost/ECMAPI/auth.json HTTP/1.1 Accept: / Content-Type: application/json X-Requested-With: XMLHttpRequest Referer: http://localhost/InterfaceGov/Default.aspx Accept-Language: en-gb Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) Host: localhost Content-Length: 43 Connection: Keep-Alive Pragma: no-cache Cookie: _ga=GA1.1.2077781525.1452005918; .ASPXANONYMOUS=hOL_MQPsZk5z5t31805S8oazFP0IdbeHKBejAH2x2Ic6hS5070aWW5A-2dklH10qT8IE9pshiAgUkmwN-TfVXjXUhAYVaWKzuOIyCy1nE8VAdETQk1TqB_39gk5ST8cIcvO2KNgB8d5h1f38W8hIzg2; ss-pid=hMBothYg2ETWObMTMJDY; ss-id=x9zxOTH2rFxLo8tO01Nw

Response auth

HTTP/1.1 200 OK Cache-Control: private Content-Type: application/json; charset=utf-8 Vary: Accept Server: Microsoft-IIS/7.5 X-Powered-By: ServiceStack/4.038 Win32NT/.NET Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Headers: Content-Type, Authorization X-AspNet-Version: 4.0.30319 Set-Cookie: ss-pid=QjEKGQphtPJZjZJmPtCt; expires=Sun, 11-May-2036 12:18:29 GMT; path=/; HttpOnly Set-Cookie: ss-id=1yKLtkiTD2NshpiPrffu; path=/; HttpOnly X-MiniProfiler-Ids: ["1d0459ec1fad4695ac0f3c7fc69cad0a","3f5cd3515e084363b86df1e54633a683","0f54d87508594cba9757660dc664aeef"] X-Powered-By: ASP.NET Date: Wed, 11 May 2016 12:18:30 GMT Content-Length: 17514

Request EventSource

GET http://localhost/ecmapi/event-stream?channel=MyChannel HTTP/1.1 Accept: / Origin: http://localhost Accept-Language: en-GB Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) Host: localhost Connection: Keep-Alive Pragma: no-cache

Response EventSource

HTTP/1.1 401 Unauthorized Cache-Control: private Vary: Accept Server: Microsoft-IIS/7.5 WWW-Authenticate: credentials realm="/auth/credentials" X-Powered-By: ServiceStack/4.038 Win32NT/.NET Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Headers: Content-Type, Authorization X-AspNet-Version: 4.0.30319 Set-Cookie: ss-pid=c0UQXy4YJNQmVuUiEVfA; expires=Sun, 11-May-2036 12:18:30 GMT; path=/; HttpOnly Set-Cookie: ss-id=y9e8BbD1na5EoHu8GUug; path=/; HttpOnly X-MiniProfiler-Ids: ["1d0459ec1fad4695ac0f3c7fc69cad0a","3f5cd3515e084363b86df1e54633a683","0f54d87508594cba9757660dc664aeef","f1584abd58e64a9dac6ceceaa932c47a"] X-Powered-By: ASP.NET Date: Wed, 11 May 2016 12:18:30 GMT Content-Length: 0

mythz

The reason why /event-stream returns a 401 Unauthorized is because your request to /event-stream does not contain the necessary Session Cookies which is what establishes an Authenticated request.

This is only an issue in IE since it's the last remaining browser that doesn't have native support for Server Sent Events and requires a SSE shim to work where one of its limitations is that it doesn't send Session Cookies.

The default_ieshim.cshtml in the http://chat.servicestack.net/default_ieshim Live Demo shows how to support Authenticated Requests in IE as well by instead appending the ss-id sessionId to the event stream url which you can populate in a Razor page with:

var source = new EventSource(
  '/event-stream?channels=@channels&ss-id=@(base.GetSession().Id)&t=' 
  + new Date().getTime());

You also need to tell ServiceStack to accept Session Id's in the Url which you can do in your AppHost's Configure() with:

SetConfig(new HostConfig {
    AllowSessionIdsInHttpParams = true,
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

sending data to particular client from twisted server

来自分类Dev

javascript:require('events')。EventEmitter;

来自分类Dev

javascript: print websocket client IP

来自分类Dev

C Client Server无法正常工作

来自分类Dev

可以使用ServiceStack.Client来使用非SS REST API吗?

来自分类Dev

TCP Server and Client java

来自分类Dev

SockJsException when websocket client connect to server

来自分类Dev

Server Sent Events in Ember JS

来自分类Dev

Java exception client authentication TLS: password must not be null

来自分类Dev

SQL Server中的ServiceStack.OrmLite Nolock提示

来自分类Dev

使用ServiceStack.Client超时

来自分类Dev

NSURLSessionTask authentication challenge completionHandler and NSURLAuthenticationChallenge client

来自分类Dev

javascript client side security

来自分类Dev

Pass multidimensional array from javascript to servicestack

来自分类Dev

将多维数组从JavaScript传递到ServiceStack

来自分类Dev

将HashSet用于带有SQL Server的ServiceStack OrmLite中的外键对象

来自分类Dev

运行Zuul Server时禁用Eureka Client

来自分类Dev

Serving personalized javascript code to a client

来自分类Dev

没有Soap Client的PHP Soap Server

来自分类Dev

带SQL Server的ServiceStack Ormlite:如何使用通配符获取where子句

来自分类Dev

Samba文件权限:Linux Server,Mac Client

来自分类Dev

将多维数组从JavaScript传递到ServiceStack

来自分类Dev

将SQL转换为ServiceStack.ORMLite SQL Server

来自分类Dev

在Servicestack.OrmLite SQL Server中使用合并时出错

来自分类Dev

Java Server / Client中的编组和解组

来自分类Dev

如何对多个Server-Client对使用ZeroMQ?

来自分类Dev

从Servicestack api元数据/ swagger生成JavaScript客户端

来自分类Dev

AdBlock ERR_BLOCKED_BY_CLIENT ... / devextreme / events / dblclick.js

来自分类Dev

Client Server tcp reset 导致丢包

Related 相关文章

  1. 1

    sending data to particular client from twisted server

  2. 2

    javascript:require('events')。EventEmitter;

  3. 3

    javascript: print websocket client IP

  4. 4

    C Client Server无法正常工作

  5. 5

    可以使用ServiceStack.Client来使用非SS REST API吗?

  6. 6

    TCP Server and Client java

  7. 7

    SockJsException when websocket client connect to server

  8. 8

    Server Sent Events in Ember JS

  9. 9

    Java exception client authentication TLS: password must not be null

  10. 10

    SQL Server中的ServiceStack.OrmLite Nolock提示

  11. 11

    使用ServiceStack.Client超时

  12. 12

    NSURLSessionTask authentication challenge completionHandler and NSURLAuthenticationChallenge client

  13. 13

    javascript client side security

  14. 14

    Pass multidimensional array from javascript to servicestack

  15. 15

    将多维数组从JavaScript传递到ServiceStack

  16. 16

    将HashSet用于带有SQL Server的ServiceStack OrmLite中的外键对象

  17. 17

    运行Zuul Server时禁用Eureka Client

  18. 18

    Serving personalized javascript code to a client

  19. 19

    没有Soap Client的PHP Soap Server

  20. 20

    带SQL Server的ServiceStack Ormlite:如何使用通配符获取where子句

  21. 21

    Samba文件权限:Linux Server,Mac Client

  22. 22

    将多维数组从JavaScript传递到ServiceStack

  23. 23

    将SQL转换为ServiceStack.ORMLite SQL Server

  24. 24

    在Servicestack.OrmLite SQL Server中使用合并时出错

  25. 25

    Java Server / Client中的编组和解组

  26. 26

    如何对多个Server-Client对使用ZeroMQ?

  27. 27

    从Servicestack api元数据/ swagger生成JavaScript客户端

  28. 28

    AdBlock ERR_BLOCKED_BY_CLIENT ... / devextreme / events / dblclick.js

  29. 29

    Client Server tcp reset 导致丢包

热门标签

归档