I am receiving CORS errors but the Messages are still being pushed to the client. The CORS Error:
Access to resource at 'https://Signalr.example.com/notificationhub?id=l3UG4AHcZNEq1sne4s76_g&access_token=xxxx' from origin 'https://webapp.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have a C# .net core web API (https://Signalr.example.com) and to an Razor Webapp (client) (https://webapp.example.com) in IIS on a local network server that has binding to those two DNS entries. Locally I use https://Localhost:5001 and https://Localhost:5002 to run these two projects without any troubles. In the webapp I am using the Microsoft Javascript client . Here is the snippet of the js code to connect to the signalR API. I am using Azure AD B2C and this works properly for authentication
var connection = new signalR.HubConnectionBuilder()
.withUrl(apiConfig.NotificationSignalRURL, { accessTokenFactory: () => response.accessToken })
.configureLogging(signalR.LogLevel.Information)
.withAutomaticReconnect()
.build();
In the web api I have set up based on all the examples I saw to solve the CORS issues. I am not sure what I am missing. SignalRAPI startup.cs Configure :
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder =>
{
builder
//.AllowAnyOrigin() ***Any Origins This doesn't work***
.WithOrigins("https://localhost:44347", "https://localhost:5001", "https://localhost:5002", "http://localhost:5003", "http://localhost:31765", "https://webapp.example.com")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
app.UseHttpsRedirection();
app.UseRouting();
//https://docs.microsoft.com/en-us/aspnet/core/signalr/security?view=aspnetcore-5.0
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/chathub");
endpoints.MapHub<NotificationHub>("/NotificationHub");
//endpoints.MapHub<NotificationUserHub>("/NotificationUserHub");
});
}