Aspnetcore.docs: What is the purpose of parameterless AddCors method?

Created on 8 Mar 2019  Â·  11Comments  Â·  Source: dotnet/AspNetCore.Docs

I can register cors services without configuring policies:

services.AddCors()

What actually happens under the covers?

Could you add the explanation to this article, please?


Document Details

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

Source - Docs.ms

Most helpful comment

@Rick-Anderson ,
This discussion makes me feel like this guy =)

off two different methods?

Yes, UseCors and AddCors. But what is for what? How does semantic of these calls differ? In both cases I am working with PolicyBuilder. Which call do I need when?

All 11 comments

@Rick-Anderson Sorry, but it didn't become clearer =(

There are overloads for both UseCors and AddCors methods which do not accept any parameters. In code snippets AddCors is always called with policies configifuration. UseCors is called differently.

@pranavkm what's the purpose of register cors services without configuring policies:

services.AddCors()

It's meant for scenarios where the application specifies a custom policy provider that isn't backed by CorsOptions.

@Rick-Anderson , would be nice to capture that in docs ;-)

@pranavkm OK if I add that to the doc?

Writing a custom policy provider isn't common and we don't expect most users to write one. AddCors is just as usable if you separately register the options type later. It's also usable if you write middleware that expects users to separately configure CORS. There could be others I'm not aware of.

I'd be happy to entertain documenting it if it were part of a coherent document that says if you were doing this scenario and happen to require CORS, here's what you would need to do. But adding docs that says you might use this overload in these non-exhaustive cases is helpful to anyone.

Thanks @pranavkm . This issue is very discoverable and will serve as the documentation.

@Rick-Anderson

In ConfigureServices I an write:

services.AddCors(options =>
{
    options.AddDefaultPolicy(builder => builder.AllowAnyOrigin().AllowAnyMethod());
});

and in Configure:

app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod());

I still do not get how do these calls relate to each other. They seem to do the same thing. Or not?

@voroninp you're calling AllowAnyOrigin().AllowAnyMethod() off two different methods.

@Rick-Anderson ,
This discussion makes me feel like this guy =)

off two different methods?

Yes, UseCors and AddCors. But what is for what? How does semantic of these calls differ? In both cases I am working with PolicyBuilder. Which call do I need when?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danroth27 picture danroth27  Â·  3Comments

Rick-Anderson picture Rick-Anderson  Â·  3Comments

fabich picture fabich  Â·  3Comments

davisnw picture davisnw  Â·  3Comments

AnthonyMastrean picture AnthonyMastrean  Â·  3Comments