Skip to content

CORS header not being set for internal server error response #22281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mo-esmp opened this issue May 27, 2020 · 6 comments
Open

CORS header not being set for internal server error response #22281

mo-esmp opened this issue May 27, 2020 · 6 comments
Labels
affected-very-few This issue impacts very few customers area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-cors This issue is related to CORS investigate Needs: Design This issue requires design work before implementating. Priority:2 Work that is important, but not critical for the release severity-nice-to-have This label is used by an internal tool triage-focus Add this label to flag the issue for focus at triage
Milestone

Comments

@mo-esmp
Copy link
Contributor

mo-esmp commented May 27, 2020

For internal server error, there are no access-control-* headers in the response. As far as I know
this issue should be fixed since ASP.NET Core 2.2.

Here is my CORS configuration:

services.AddCors(options =>
{
    options.AddPolicy(name: "AllowedOrigins",
        policyBuilder =>
        {
            var urls = Configuration.GetSection("Host:AllowedOrigins").Get<List<string>>();
            policyBuilder.WithOrigins(urls.ToArray())
                .AllowAnyMethod()
                .AllowAnyHeader()
                .SetIsOriginAllowed((host) => true)
                .AllowCredentials();
        });
});

And in Configure method :

app.UseRouting();

app.UseCors("AllowedOrigins");
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

Further technical details

- ASP.NET Core version: 3.1

.NET Core SDK (reflecting any global.json):
 Version:   3.1.202
 Commit:    6ea70c8dca

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.202\

Host (useful for support):
  Version: 3.1.4
  Commit:  0c2e69caa6

.NET Core SDKs installed:
  1.0.0 [C:\Program Files\dotnet\sdk]
  2.1.802 [C:\Program Files\dotnet\sdk]
  2.2.402 [C:\Program Files\dotnet\sdk]
  3.1.200 [C:\Program Files\dotnet\sdk]
  3.1.202 [C:\Program Files\dotnet\sdk]
- Visual Studio 15.6
@pranavkm pranavkm added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-cors This issue is related to CORS labels May 27, 2020
@sackri10
Copy link

sackri10 commented May 28, 2020

After seeing the Code fix done as part of Issue #2378. I feel like the fix is not considered for latest code.

@mkArtakMSFT
Copy link
Contributor

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@javiercn javiercn added affected-very-few This issue impacts very few customers severity-nice-to-have This label is used by an internal tool enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Mar 19, 2021 — with ASP.NET Core Issue Ranking
@Entroper
Copy link

Entroper commented Jun 8, 2021

I know this seems insignificant, but this is a thorn in my side when helping newer developers debug. Every 500, 404, 401/403 etc. is reported by the browser as a CORS error. A lot of time is wasted chasing these red herrings when the real issue has nothing to do with CORS.

@pranavkm pranavkm modified the milestones: Backlog, .NET 7 Planning Oct 19, 2021
@mkArtakMSFT mkArtakMSFT added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Nov 11, 2021
@rafikiassumani-msft rafikiassumani-msft added triage-focus Add this label to flag the issue for focus at triage Priority:2 Work that is important, but not critical for the release Cost:L Needs: Design This issue requires design work before implementating. labels Jan 11, 2022
@pavlexander
Copy link

hey, I've been struggling with the same issue and following solution seems to have worked:

app.UseExceptionHandler(exceptionHandlerApp =>
{
    exceptionHandlerApp.Run(async context =>
    {
        context.Response.StatusCode = StatusCodes.Status500InternalServerError;
    });
});

app.UseCors(...);

I have made an answer on StackOverflow https://stackoverflow.com/a/71695813/1215913

There is also an explanation provided by another author why the solution works.

Happy coding :)

@amcasey amcasey added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Jun 2, 2023
@captainsafia captainsafia removed the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jun 20, 2023
@lnxph-devops-sareno
Copy link

I had the same issue. As a workaround, I added a new middleware that act as a global exception handler:

var app = builder.Build();

app.Use(async (context, next) =>
{
        try
        {
            await next(context);
        }
        catch (Exception e)
        {
            context.Response.StatusCode = StatusCodes.Status500InternalServerError;
            await context.Response.WriteAsync(e.ToString());
        }
});

app.UseCors();

@filipbekic01
Copy link

We spent hours and hours trying to figure out what's wrong with internal server response. First we thought it's our proxy cutting the response but no, it's dotnet. Hope for the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affected-very-few This issue impacts very few customers area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-cors This issue is related to CORS investigate Needs: Design This issue requires design work before implementating. Priority:2 Work that is important, but not critical for the release severity-nice-to-have This label is used by an internal tool triage-focus Add this label to flag the issue for focus at triage
Projects
None yet
Development

No branches or pull requests