thegrid
thegrid

Reputation: 514

CORS settings having no effect on AWS Lambda WebApi 2.0 Controllers in local dev environment

I have a very peculiar problem.

  1. I am developing a serverless AWS Lambda based WebAPI (.NET core 2.0).
  2. I added a controller and all the required CORS settings in the Setup.cs.
  3. I was successfully able to use the API after all the above steps.
  4. I went ahead and added a second controller and the CORS settings had no effect!
  5. I tried multiple methods such as adding the EnableCors to the controllers explicitly and adding an interceptor for the OPTIONS but again no effect.
  6. I removed the additional controller and everything is back to normal (CORS has an effect).

Is there a restriction on CORS and the number of controllers in a single Lambda function for the .NET core?

Edit 1 : Adding code segments for better illustration

In ConfigureServices

var origins = Configuration.GetSection("AllowedCORS").AsEnumerable().Select(x => x.Value);
services.AddCors(o => o.AddPolicy("AllOrigins", builder =>
{
    builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
}));

Then in Configure

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseCors("AllOrigins");
    app.UseAuthentication();
    app.UseMvc();          
}

Upvotes: 1

Views: 439

Answers (1)

thegrid
thegrid

Reputation: 514

I managed to resolve the issue by manually adding an Options handler in the controller and returning 200 OK.

Lambda does allow users to enable CORS but for some reason that method did not work for me (too many errors during setup)

At present i am restricting one Lambda function to one controller thereby giving me the freedom to modify only a certain set of the APIs.

Upvotes: 1

Related Questions