'ASP.NET and SignalR CORS issue

I working with ASP.NET and now I'm adding SignalR to my project. I'm consuming a Web API from another origin using Angular. Now I want to use SignalR in Angular, but I'm getting cors error for cross origin but Web API is working

Error:

Request URL: https://localhost:44319/SignalRServer/negotiate
Referrer Policy: strict-origin-when-cross-origin
Provisional headers are shown
Learn more
Content-Type: text/plain;charset=UTF-8
DNT: 1
Referer: http://localhost:4200/ sec-ch-ua: "Microsoft Edge";v="93", " Not;A Brand";v="99", "Chromium";v="93" sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63
Safari/537.36 Edg/93.0.961.38
X-Requested-With: XMLHttpRequest

[assembly: OwinStartup(typeof(TestClientDotNetFramework.Startup))]
namespace TestClientDotNetFramework
{
    public class Startup  
    {
        // GET api/<controller>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
                 {
                    builder.AllowAnyHeader()
                           .AllowAnyMethod()
                           .SetIsOriginAllowed((host) => true)
                           .AllowCredentials();
                 }));
        }

        public void Configuration(IAppBuilder app)
        {
            app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);
                map.RunSignalR(new HubConfiguration { EnableJSONP = true });
            });

          var corsPolicy = new CorsPolicy
            {
                AllowAnyMethod = true,
                AllowAnyHeader = true,
                SupportsCredentials = true,
            };
            corsPolicy.Origins.Add("http://localhost:4200");

            var corsOptions = new CorsOptions
            {
                PolicyProvider = new CorsPolicyProvider
                {
                    PolicyResolver = context =>
                    {
                        context.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
                        return Task.FromResult(corsPolicy);
                    }
                }
            };
           
            app.Map("/SignalRServer", map =>
            {
                map.UseCors(corsOptions);
                var config = new HubConfiguration();
                config.EnableDetailedErrors = true;
                map.RunSignalR(config);
            });

        }
    }
} 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source