简体   繁体   English

有没有一种方法可以只为ASP.NET Core 2.2 MVC中的SignalR集线器设置CORS策略?

[英]Is there a way to set CORS policy solely for a SignalR hub in ASP.NET Core 2.2 MVC?

I have an ASP.NET Core 2.2 MVC project and I need to set CORS policy specifically for SignalR hub but not globally (not for all controllers and actions). 我有一个ASP.NET Core 2.2 MVC项目,我需要专门为SignalR集线器设置CORS策略,但不能全局设置(并非为所有控制器和动作设置)。 If I follow recommendations by adding app.UseCors() it works but globally and I need to apply the policy only for SignalR. 如果我通过添加app.UseCors()遵循建议,那么它可以工作,但在全球范围内,我只需要对SignalR应用该策略。 Is there a way to do that? 有没有办法做到这一点?

I have tried this too but with no success. 我已经试过太,但没有成功。

EDIT 2019-07-22 18:33 CEST 编辑2019-07-22 18:33 CEST

Here is CORS policy I use: 这是我使用的CORS政策:

services.AddCors(o =>
            {
                o.AddPolicy("MyCorsPolicy", b =>
                {
                    b.AllowAnyHeader()
                    .AllowAnyMethod()
                    .SetIsOriginAllowed(s => true)
                    .AllowCredentials();
                });
            });

After hours of attempts I had to turn to GitHub and this is what Brennan Conroy suggested: https://github.com/aspnet/AspNetCore/issues/12564 经过数小时的尝试,我不得不转向GitHub,这就是Brennan Conroy的建议: https : //github.com/aspnet/AspNetCore/issues/12564

And it works! 而且有效!

Edit 2019-07-29 13:40 CEST 编辑2019-07-29 13:40 CEST

This is what helped: 这是有帮助的:

app.Map("/path", c =>
{
    c.UseCors("somePolicy");
    c.UseSignalR(route =>
    {
        route.MapHub<MyHub>("/hub");
    });
});

And clients will connect to the hub via "/path/hub". 客户端将通过“ / path / hub”连接到集线器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM