我已经建立了一个温泉网站的角在前端和asp.net核心WebApi3.0在服务器端。
我正在尝试配置Windows身份验证,感谢这篇文章到目前为止还不错!然而..。当Api正确返回401/403时,CORS头出现问题,cors头丢失。下面是一个示例:
General:
Status Code: 403 Forbidden
Remote Address: [::1]:44389
Referrer Policy: no-referrer-when-downgrade
Response-Headers:
Date: Wed, 20 Nov 2019 16:33:57 GMT
Persistent-Auth: true
Server: Microsoft-IIS/10.0
Transfer-Encoding: chunked
X-Powered-By: ASP.NET这就是当我使用[Authorise]属性时生成的内容。我遇到的问题是,当CORS头丢失时,角内的状态代码为0,所以我的http拦截器不知道不授权,无法正确地处理重定向。
我尝试过在StartUp中使用中间件手动添加报头。
app.Use(async (context, next) =>
{
context.Response.OnStarting(() =>
{
context.Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:4200");
return Task.FromResult(0);
});
await next();
});但这是行不通的,中间的软件仍然删除标题。
有趣的是,如果我删除authorise属性并在控制器方法中返回401/403:
[HttpGet]
public IActionResult Get()
=> Unauthorized();CORS标头按如下方式返回:
Response-Headers:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:4200 这可不行!
有人知道如何确保授权中间件持久化CORS头吗?
发布于 2019-11-21 10:55:44
事实证明一切都配置正确,好吧,几乎!
问题只是中间件在StartUp类中配置的顺序。我不知道中间软件配置的顺序对执行有影响。这是我的原始代码:
app.UseRouting();
app.UseAuthentication()
.UseAuthorization();
app.UseCors(builder => builder.WithOrigins("http://localhost:4200")
.AllowCredentials()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });配置Cors 之前身份验证解决了问题,现在未经授权的请求处理的中间软件仍然包括cors头。
app.UseRouting();
app.UseCors(builder => builder.WithOrigins("http://localhost:4200")
.AllowCredentials()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthentication()
.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });发布于 2019-11-26 20:09:59
你能把完整的startup.cs文件的副本寄出去吗?我也有同样的问题,并跟随你的变化,但仍然收到401。我试图访问IIS 10上设置为windows身份验证的站点。
https://stackoverflow.com/questions/58959419
复制相似问题