Skip to content

CORS preflight doesn't work with OData in ASP.NET Core #1611

Closed

Description

@bitsforthought

Sending CORS preflight requests to an OData service running on ASP.NET Core fails with 404 when it shouldn't. Note that this is a different symptom than this similar issue, and also likely a different cause since the implementation of CORS in ASP.NET Core is substantially different than the one for classic ASP.NET.

Assemblies affected

Microsoft.AspNetCore.OData 7.0.1

Reproduce steps

I managed to reproduce this by creating a solution with two projects: One ASP.NET Core Web API project, and one MSTest-based unit test project. For some reason, I couldn't get ASP.NET Core to work at all if I combined all the code in a single project.

The repro is here

Expected result

All four tests pass

Actual result

The test that uses CORS with OData fails. The first preflight request returns 404 instead of 200.

Additional detail

I haven't been able to determine the root cause of this. The failure mode is different than it is in classic ASP.NET.

Activity

  1. cornem commented on Sep 28, 2018

    @cornem

    Related to aspnet/CORS#98?

    A possible workaround is to let middleware handle the CORS pre-flight requests (but it means less granular control over CORS policies):

    services.AddCors(options =>
    {
    	options.AddPolicy(
    		"AllowAllOrigins",
    		builder => builder
                            .AllowAnyOrigin()
                            .AllowAnyHeader()
                            .AllowAnyMethod()
                            .AllowCredentials());
    
    	options.DefaultPolicyName = "AllowAllOrigins";
    });
    

    And adding app.UseCors() before the call to UseMvc().

    The ODataRequestMethod enum doesn't know Options, so Unkown is returned here. As a result EntitySetRoutingConvention doesn't find a matching action.

    I have done a local test where I've set the method to whatever is passed in the Access-Allow-Control-Method header, and then the result will be status code 200 (although I suppose it should actually be 204). Inserted at L83 of WebApiRequestMessage:

    if (method.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase) 
    	&& innerRequest.Headers.ContainsKey("Access-Control-Request-Method"))
    {
    	method = innerRequest.Headers["Access-Control-Request-Method"]
    		.First().ToUpperInvariant();
    }
    
  2. JoshZA commented on Feb 2, 2019

    @JoshZA

    👍

  3. TehWardy commented on Jul 15, 2019

    @TehWardy

    Tell me about it ...

    I wish there was a way I could just say "See that OPTIONS header and just return OK" as CORS is a blight on the internet.

    All it does is force all clients to hit the servers twice since there's literally no way to verify the information in the request is valid it boils down to trusting the client to tell you they care about this and are giving you the right information. Much like the EU cookie directive ... this feature needs to die!

    Rant over ... In the meantime ...

    I would like a way to do something like "app.UseCors().AllowAnything()" and just have any request from any browser accept that all OPTIONS request have come back positive regardless.

    Right now the only way I know of to get this behavior is to literally put in custom middle-ware to look for the OPTIONS header and respond then end the request but that's an ugly hack.

  4. marabooy commented on Oct 1, 2020

    @marabooy
    Member

    Closing this as a it is fixed now.

  5. sherlock1982 commented on Mar 29, 2022

    @sherlock1982

    Still doesn't work for me.
    In Preflight "OPTIONS" I get 405 with a header: "allow: DELETE, GET, PATCH"
    Tried to remove "app.UseCors()", the same thing.

    In the logs I see:
    "No CORS policy found for the specified request." (this is with app.UseCors())
    or just:
    "Executing endpoint '405 HTTP Method Not Supported'" (this is without app.UseCors())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions