AspNetCore.SignalR 2.1 and CORS

前端 未结 6 1383
梦谈多话
梦谈多话 2020-12-29 06:49

I\'m migrating our SignalR-Service to the new AspNetCore.SignalR (2.1 preview) and now I get problems with CORS. I will never access the service from the same origin, so I n

相关标签:
6条回答
  • 2020-12-29 07:01

    I had the same issue, I use reactjs for clientside. Changing the version from using "@aspnet/signalr-client" into "@aspnet/signalr" solved my problem

    0 讨论(0)
  • 2020-12-29 07:03

    I solved the problem.

    The client-package and the server-package were not in the exact subversion state. It was 2.1-preview3210 and 2.1-preview3209 (example numbers).

    The server-package is published to the nuget feed frequently but the client-package (npm) not. So I cloned the latest git repo by myself and build the libs and it works.

    You will not have the problem anymore, the client-package is now up-to-date.

    0 讨论(0)
  • 2020-12-29 07:04

    This didn't work for me and a subtly different version did -

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      ...
      app.UseCors(builder => builder
        .AllowAnyHeader()
        .AllowAnyMethod()
        .SetIsOriginAllowed((host) => true)
        .AllowCredentials()
      );
    
      app.UseSignalR(routes => { routes.MapHub<ChatHub>("/chatHub"); });
      app.UseMvc();
    }
    
    0 讨论(0)
  • 2020-12-29 07:05

    You have to upgrade your package to @aspnet/signalr when upgrading to preview1:

    NOTE: Previous previews of the SignalR client library for JavaScript were named @aspnet/signalr-client. This has been deprecated as of Preview 1.

    source: Github readme

    0 讨论(0)
  • 2020-12-29 07:07

    If you're migrating your project to asp.net core version 3.x, you can upgrade routing from the Configure method like this:

    app.UseCors("AllowCors");
    
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    
        endpoints.MapHub<TestHub>("/test");
    });
    

    app.UseMvc and app.UseSignalR will be replaced with app.UseEndpoints. app.UseSignalR also cannot be used anymore because:

    This method is obsolete and will be removed in a future version.

    0 讨论(0)
  • 2020-12-29 07:13

    Try to move app.UseSignalR() on top of app.UseMvc() like this

    app.UseCors("AllowCors");
    app.UseSignalR(routes =>
        {
            routes.MapHub<TestHub>("/test");
        });
    app.UseMvc();
    
    0 讨论(0)
提交回复
热议问题