コード例 #1
0
        public async Task Invoke(HttpContext context, IAppService appService, IConfigService configService)
        {
            if (context.Request.Path == "/ws")
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var basicAuth = new BasicAuthenticationAttribute(appService);
                    if (!await basicAuth.Valid(context.Request))
                    {
                        await context.Response.WriteAsync("closed");

                        return;
                    }
                    var       appId     = context.Request.Headers["appid"];
                    WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var client = new WebsocketClient()
                    {
                        Client            = webSocket,
                        Id                = Guid.NewGuid().ToString(),
                        AppId             = appId,
                        LastHeartbeatTime = DateTime.Now
                    };
                    _websocketCollection.AddClient(client);
                    _logger.LogInformation("Websocket client {0} Added ", client.Id);
                    try
                    {
                        await Handle(context, client, configService);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Handle websocket client {0} err .", client.Id);
                        await _websocketCollection.RemoveClient(client, WebSocketCloseStatus.Empty, ex.Message);

                        await context.Response.WriteAsync("closed");
                    }
                }
                else
                {
                    context.Response.StatusCode = 400;
                }
            }
            else
            {
                await _next(context);
            }
        }
コード例 #2
0
        public async Task ValidTest()
        {
            var service = new Mock <IAppService>();
            App app     = null;

            service.Setup(s => s.GetAsync("01")).ReturnsAsync(app);
            service.Setup(s => s.GetAsync("02")).ReturnsAsync(new App());
            service.Setup(s => s.GetAsync("03")).ReturnsAsync(new App()
            {
                Secret = "1",
            });
            service.Setup(s => s.GetAsync("app01")).ReturnsAsync(new App()
            {
                Secret  = "11",
                Enabled = true
            });

            var http   = new DefaultHttpContext();
            var filter = new BasicAuthenticationAttribute(service.Object);
            var result = await filter.Valid(http.Request);

            Assert.IsFalse(result);
            http.Request.Headers["appid"] = "01";
            result = await filter.Valid(http.Request);

            Assert.IsFalse(result);
            http.Request.Headers["appid"] = "02";
            result = await filter.Valid(http.Request);

            Assert.IsTrue(result);
            http.Request.Headers["appid"]         = "03";
            http.Request.Headers["Authorization"] = "Basic YXBwMDE6MTE=";
            result = await filter.Valid(http.Request);

            Assert.IsFalse(result);
            http.Request.Headers["appid"]         = "app01";
            http.Request.Headers["Authorization"] = "Basic YXBwMDE6MTE=";
            result = await filter.Valid(http.Request);

            Assert.IsTrue(result);
        }