SignalR / SignalR

Incredibly simple real-time web for .NET
http://signalr.net
Other
9.2k stars 2.28k forks source link

CORS Problem when trying to connet from static delivered file #4725

Closed Salmakis closed 1 week ago

Salmakis commented 2 weeks ago

Howdy, i try to use SignalR from static delivered HTML/JS Files

But somehow it wont work after all of try and error, trying to sort the middlewares and stuff. What am i doing wrong?

Expected behavior

It Should be able to cennect with signalR

Actual behavior

It ts not possible to connect due CORS problems

Steps to reproduce

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy", policyBuilder => policyBuilder
                .WithOrigins(
                    "http://localhost:5175",
                    "https://localhost:5175")
                .SetIsOriginAllowedToAllowWildcardSubdomains()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());

            options.DefaultPolicyName = "CorsPolicy";
        });
        builder.Services.AddSignalR(options =>
        {
            options.EnableDetailedErrors = true;
        });
        var app = builder.Build();

        app.UseCors("CorsPolicy");
        app.UseRouting();
        app.UseEndpoints(endpoints => endpoints.MapHub<TestHub>("/test"));
        app.UseDefaultFiles();
        app.UseStaticFiles(new StaticFileOptions
        {
            ServeUnknownFileTypes = true
        });

        app.Run();
    }
}


* see an connection refuse due to CORS policy

i extra dont used https to make sure its not a Certificate Problem.
BrennanConroy commented 1 week ago

Hi. It looks like this is a question about how to use ASP.NET Core. While we do our best to look through all the issues filed here, we are not a general-purpose forum. To get a faster response we suggest posting your questions to StackOverflow using the asp.net-core tag.