我在我的Web应用程序中遇到了CORS问题,该应用程序是用Angular(TypeScript)构建的,使用C#(.NET 8)API服务器。虽然GET方法工作正常,但POST、PUT和POST方法似乎不工作,我在浏览器中收到了CORS错误。
下面是我负责CORS配置的部分代码:
Angular TypeScript
deleteAdmRole(key: string): Observable<void> {
return this.http.delete<void>(`${this.apiUrl}/Delete/${key}`, { withCredentials: true })
.pipe(
catchError((error) => {
console.error(`Error deleting AdmRole with key ${key}:`, error);
return throwError(() => error);
})
);
}
字符串
Program.cs
const string myAllowSpecificOrigins = "_myAllowSpecificOrigins";
services.AddCors(options =>
{
options.AddPolicy(name: myAllowSpecificOrigins,
builder =>
{
builder.WithOrigins(
"https://localhost:44498",
"https://localhost:44337",
"http://localhost:44498",
"http://localhost:44337")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
app.UseCors(myAllowSpecificOrigins);
型
控制器C# .NET 8
[ApiController]
[Route("api/[controller]")]
public class AdmApiController : Controller
{
[HttpGet]
[Route(nameof(Get))]
public async Task<IActionResult> Get(DataSourceLoadOptions loadOptions)
{
[HttpDelete]
[Route(nameof(Delete) + "/{key}")]
public async Task Delete(Guid key)
{
型
当我尝试在Angular中调用css方法时,我在浏览器中收到以下CORS错误:
Access to XMLHttpRequest at 'https://localhost:44337/api/AdmApi/Delete?key=619bd863-5559-42e1-82ba-9c3b544a361b' from origin 'https://localhost:44498' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
型
有人能帮我确定我的CORS配置可能有什么问题吗?或者我如何解决这个问题,使POST,PUT和POST方法正确工作?提前感谢您的帮助!
编辑,回答问题
执行程序的顺序.cs
Services.EntityFramework.Configure(builder.Services, builder.Configuration, builder.Environment);
Services.IocContainer.Configure(builder.Services);
Services.HttpClientFactory.Configure(builder.Services, builder.Configuration);
Services.CookiePolicy.Configure(builder.Services, builder.Configuration);
Services.Cors.Configure(builder.Services);
Services.Authentication.Configure(builder.Services);
Services.Mvc.Configure(builder.Services);
Services.Session.Configure(builder.Services);
Middleware.HttpsRedirection.Apply(app);
Middleware.Routing.Apply(app);
Middleware.Cors.Apply(app);
Middleware.Authentication.Apply(app);
Middleware.Endpoints.Apply(app);
Middleware.CookiePolicy.Apply(app);
型
与
[HttpDelete(nameof(Delete) + "/{key}")]
public async Task Delete(Guid key)
{
型
我有同样的问题
{
"headers": {
"normalizedNames": {},
"lazyUpdate": null,
"headers": {}
},
"status": 0,
"statusText": "Unknown Error",
"url": "https://localhost:44337/api/AdmApi/Delete/619bd863-5559-42e1-82ba-9c3b544a361b",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure response for https://localhost:44337/api/AdmApi/Delete/619bd863-5559-42e1-82ba-9c3b544a361b: 0 Unknown Error",
"error": {
"isTrusted": true
}
}
型
不使用SetIsOriginAllowed(origin => true)
我的launchsettings.json:
{
"profiles": {
"SKWangular": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
},
"applicationUrl": "https://localhost:44337",
"windowsAuthentication": true,
"anonymousAuthentication": false
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
},
"windowsAuthentication": true,
"anonymousAuthentication": false
}
},
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:1285",
"sslPort": 44337
}
}
}
型
第一个配置文件“SKWangular "允许GET、POST、PUT和OPTION的CORS设置,但它不启用Windows身份验证。第二个配置文件”IIS Express“只允许GET;其他方法不起作用并导致CORS错误,但它启用了Windows身份验证。如何配置它以使这两种功能一起工作?
第一个配置文件,'SKWangular:为什么?
HttpContext.User.Identity --- IsAuthenticated = false
型
1条答案
按热度按时间wnrlj8wa1#
尝试使用以下本地主机:chrome://flags/#temporary-unexpire-flags-m118
here is the image description的