C
C#14mo ago
niels

✅ Cors not adding headers on ASP.NET application

I have a asp.net core web app, that will receive calls from a different origin (different port). To test, I have the following code in the Program.cs of the ASP.NET application. var app = builder.Build(); app.UseCors(builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }); app.UseAuthentication(); app.UseAuthorization(); app.CreateDbIfNotExists(); // Configure the HTTP request pipeline. app.MapGrpcService<PatientServiceGrpc>(); app.MapGrpcService<AuthenticateServiceGrpc>(); app.MapGrpcService<AdminServiceGrpc>(); app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); app.Run(); However, I can't make calls to the api through the frontend, since the responses don't contain the Access-Control-Allow-Origin header, even though I allow all origins. What am I missing here?
5 Replies
Omnissiah
Omnissiah14mo ago
using .WithOrigins() for reading origins from appsettings is easy to implement, if you want to avoid AllowAnyOrigin or test/debug a different approach but i wouldn't know how to check that cors is enabled on minimal apis i have only classic controllers when i use EnableCors attribute
niels
nielsOP14mo ago
I know, the issue is that even with allowing all origins, it doesn't work. Maybe because I'm using GRPC.
niels
nielsOP14mo ago
I followed these instructions here: https://learn.microsoft.com/en-us/aspnet/core/grpc/grpcweb?view=aspnetcore-7.0 , but no dice so far...
gRPC-Web in ASP.NET Core gRPC apps
Learn how to configure gRPC services on ASP.NET Core to be callable from browser apps using gRPC-Web.
niels
nielsOP14mo ago
Ah, fixed it. Needed to also support HTTP/1 Jay 😄
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server