Skip to content

Commit 359f79b

Browse files
DangDang
authored andcommitted
Fix CORS configuration error - wildcard origins with credentials
Fixed InvalidOperationException: CORS protocol does not allow wildcard origins with credentials - Updated CORS policy to handle wildcard vs specific origins properly - Wildcard origins: AllowAnyOrigin() without credentials - Specific origins: WithOrigins() with credentials enabled - Added logging to show which CORS policy is being used - Updated Docker and Production configs with specific origins - Application should now start successfully in Docker containers
1 parent 8ad5c55 commit 359f79b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

VehicleShowroomManagement/src/WebAPI/Program.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,27 @@
178178
{
179179
options.AddPolicy("AllowFE", policy =>
180180
{
181-
policy.WithOrigins(builder.Configuration["Cors:Origins"]?.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? ["http://localhost:3000"])
182-
.AllowAnyMethod()
183-
.AllowAnyHeader()
184-
.AllowCredentials();
181+
var origins = builder.Configuration["Cors:Origins"]?.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? ["http://localhost:3000"];
182+
183+
Log.Information("CORS Origins configured: {Origins}", string.Join(", ", origins));
184+
185+
if (origins.Contains("*"))
186+
{
187+
// For wildcard origins, don't allow credentials
188+
Log.Information("Using wildcard CORS policy (no credentials)");
189+
policy.AllowAnyOrigin()
190+
.AllowAnyMethod()
191+
.AllowAnyHeader();
192+
}
193+
else
194+
{
195+
// For specific origins, allow credentials
196+
Log.Information("Using specific origins CORS policy (with credentials)");
197+
policy.WithOrigins(origins)
198+
.AllowAnyMethod()
199+
.AllowAnyHeader()
200+
.AllowCredentials();
201+
}
185202
});
186203
});
187204

VehicleShowroomManagement/src/WebAPI/appsettings.Docker.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
},
1818
"Cors": {
19-
"Origins": "*"
19+
"Origins": "http://localhost:3000;http://localhost:3001;http://localhost:8080;http://localhost:8081"
2020
},
2121
"Kestrel": {
2222
"Endpoints": {

VehicleShowroomManagement/src/WebAPI/appsettings.Production.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
}
1616
},
1717
"Cors": {
18-
"Origins": "*"
18+
"Origins": "https://yourdomain.com;https://www.yourdomain.com"
1919
}
2020
}
Binary file not shown.

0 commit comments

Comments
 (0)