gpt4 book ai didi

asp.net-core - Asp.NET MVC 核心 CORS

转载 作者:行者123 更新时间:2023-12-04 15:19:42 28 4
gpt4 key购买 nike

我们正在开发具有移动部分和 Web UI 的应用程序。 Web UI 使用 angular,我们在后端配置 cors 时遇到问题。我们的代码看起来像这样(只是对我们的问题很重要的代码):

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCors("AllowAll");

app.UseMvc();
}

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

//Add Cors support to the service
services.AddCors(
options => options.AddPolicy(
"AllowAll", p => p.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
)
);
}

从文档和其他关于 stackoverflow 的帖子来看,这应该可以工作,但不能。我们错过了什么?

谢谢

编辑:

这是来自 POSTMAN 的请求:

curl 'https://guw.azurewebsites.net/api/token' -X OPTIONS -H 'Pragma: no-cache' -H 'Access-Control-Request-Method: POST' -H 'Origin: http://localhost:9000' -H 'Accept-Encoding: gzip, deflate, sdch, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' -H 'Accept: /' -H 'Cache-Control: no-cache' -H 'Referer: http://localhost:9000/' -H 'Connection: keep-alive' -H 'Access-Control-Request-Headers: accept, authorization, content-type' --compressed



您可以在 postman 中导入它并查看它。此请求由 angular 应用程序发送。

希望这可以帮助。

最后我决定在我的中间件中添加这个方法:
private void AddCors(HttpContext context)
{
context.Response.Headers.Add("Access-Control-Allow-Headers",
new string[] { "Authorization", "Content-Type" });
context.Response.Headers.Add("Access-Control-Allow-Methods",
new string[] { "OPTIONS", "POST", "GET", "DELETE", "PUT" });

IEnumerable<string> allowedUrls = new List<string>()
{
"http://localhost:8100",
"http://localhost:9000"
};

if (allowedUrls.Count() > 0)
{
foreach(string x in allowedUrls)
{
var origin = context.Request.Headers.FirstOrDefault(
key => key.Key == "Origin");
var found = x.ToLower() == origin.Value.ToString().ToLower();
if (found)
{
context.Response.Headers.Add("Access-Control-Allow-Origin",
new string[] { origin.Value });
}
}
}

return;
}

编辑:

这很好,但在逻辑上它没有按我需要的那样工作,所以我在我的中间件类中结束了这个,这工作正常:
// Add CORS to every response
context.Response.Headers.Add("Access-Control-Allow-Headers",
new string[] { "Authorization", "Content-Type" });
context.Response.Headers.Add("Access-Control-Allow-Methods",
new string[] { "OPTIONS", "POST", "GET", "DELETE", "PUT" });
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");

if (context.Request.Method.Equals("OPTIONS", StringComparison.Ordinal))
{
context.Response.StatusCode = 204;
return _next(context);
}

谢谢

最佳答案

在我的应用程序(以 angular2 作为前端)中,我按照以下方式进行 -

        app.UseCors(builder => {
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});

请参阅此了解更多信息 - https://docs.asp.net/en/latest/security/cors.html

看看这是否有帮助。

关于asp.net-core - Asp.NET MVC 核心 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38838006/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com