When I Wanted to add Cors, there were 2 options. Host
and Origin
.
What is the difference between them?
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowOrigin", builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true)
.AllowCredentials();
//or
builder
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed((host) => true)
.AllowCredentials();
});
});
CodePudding user response:
Those are both identical.
SetIsOriginAllowed
expects a function that takes a string and returns a boolean.
Both of those lambdas do that.
You can call the parameter "x" and it'll still be the same function.
CodePudding user response:
The method here is .SetIsOriginAllowed(Func<bool,string>)
.
It's one method that takes a lambda with a string parameter that returns a bool. If you're not familiar with lambdas, they can be thought of as really terse inline functions.
In your question, host
and origin
are just different names for that string parameter, you could call it TheSpanishInquisition
and it would still work identically. If you're not actually using that parameter, I'd suggest using the _
discard operator, so it would be:
builder
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(_ => true)
.AllowCredentials();
The different bracketisation doesn't matter either. Brackets around lambda parameters are optional if there's only a single parameter but are required if there are multiple.