Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, WsRouter router, JwtManager jwtManager)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseCors(builder => builder
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());

            app.UseWebSockets();
            app.Use(async(context, next) =>
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    // 先检查有没有Token附在ws上
                    var reqUrl = new Url(context.Request.GetDisplayUrl());
                    if (!reqUrl.QueryParams.ContainsKey("did"))
                    {
                        context.Response.StatusCode = 400;
                        return;
                    }
                    if (!reqUrl.QueryParams.ContainsKey("token"))
                    {
                        context.Response.StatusCode = 401;
                        return;
                    }
                    string jwtToken = reqUrl.QueryParams["token"].ToString();
                    if (jwtManager.ValidateJwtToken(jwtToken, tokenValidationParameters) == null)
                    {
                        context.Response.StatusCode = 401;
                        return;
                    }
                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();
                    await router.Route(context.Request.Path.Value, context, webSocket);
                }
                else
                {
                    await next();
                }
            });
            app.UseMvc();
        }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, WsRouter router)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseWebSockets();
            app.Use(async (context, next) =>
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();
                    var url = context.Request.Path.Value;
                    await router.Route(url, context, webSocket);
                }
                else 
                {
                    await next();
                }
            });
            app.UseMvc();
        }