有没有办法在 ASP.NET Core 2.2 MVC 中仅为 SignalR 集线器设置 CORS


我有一个 ASP.NET Core 2.2 MVC 项目,我需要专门为 SignalR 集线器设置 CORS 策略,但不是全局设置(不适用于所有控制器和操作)。如果我按照建议添加app.UseCors()它,但在全球范围内,我只需要为 SignalR 应用该策略。有没有办法做到这一点?

编辑 2019-07-22 18:33 中欧夏令时

这是我使用的 CORS 策略:

services.AddCors(o =>

            {

                o.AddPolicy("MyCorsPolicy", b =>

                {

                    b.AllowAnyHeader()

                    .AllowAnyMethod()

                    .SetIsOriginAllowed(s => true)

                    .AllowCredentials();

                });

            });


汪汪一只猫
浏览 195回答 1
1回答

偶然的你

经过几个小时的尝试,我不得不求助于 GitHub,这就是 Brennan Conroy 的建议:https ://github.com/aspnet/AspNetCore/issues/12564它有效!编辑 2019-07-29 13:40 中欧夏令时这就是有帮助的:app.Map("/path", c =>{&nbsp; &nbsp; c.UseCors("somePolicy");&nbsp; &nbsp; c.UseSignalR(route =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; route.MapHub<MyHub>("/hub");&nbsp; &nbsp; });});客户端将通过“/path/hub”连接到集线器。
打开App,查看更多内容
随时随地看视频慕课网APP