Skip to content

Add content about enabling CORS for SignalR without applying the policy globally. #12918

@ryan-h

Description

@ryan-h

I encountered a situation where the CORS policy used for SignalR could not be enabled as the middleware default for the entire application. Instead I needed to use a specific policy only for the requests to the SignalR hubs.

Maybe it would be helpful to add a subsection about "Cross-origin resource sharing" that describes how to branch the request pipeline in order to apply a CORS policy specifically for SignalR, which would then allow an application to use attributes to enable CORS elsewhere instead of using middleware.

public void Configure(IApplicationBuilder app)
{
    ...

    app.Map("/hubs", config =>
    {
         // Must be called before mapping SignalR hubs
         config.UseCors(builder =>
         {
             builder.WithOrigins("https://myorigin:5000")
                 .WithMethods("Get", "Post")
                 .AllowAnyHeader()
                 .AllowCredentials();
         });

         config.UseSignalR(routes =>
         {
             routes.MapHub<ChatHub>("/chat");
         });
    });
}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.


Associated WorkItem - 566444

Metadata

Metadata

Labels

SignalRSource - Docs.msDocs Customer feedback via GitHub Issueai-backlog-handling-pre-1-1-26Used for a 2026 project for identifying old backlog issues copilot could handleai-triage-action-planAssigned when an AI generated triage and action plan report is created for an issue.doc-enhancementseQUESTeredIdentifies that an issue has been imported into Quest.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions