FRZ*_*RZ7 6 javascript windows-authentication cors asp.net-web-api preflight
我在 ASP.net Core 2(Windows 身份验证)上有一个 API,在 angular 上有一个前端。我进行了 cors 配置以从 SPA 角度查询我的后端,但由于预检而被 IIS 服务器拒绝,因为他没有身份信息而被阻止。
错误信息 :
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://XXXXXX' is therefore not allowed access. The response had HTTP status code 401.
Run Code Online (Sandbox Code Playgroud)
代码侧正面:
//MY HEADER
private headers = new Headers({
'Content-Type': 'application/json',
'Access-Control-Allow-Credentials':'true',
'Access-Control-Allow-Origin':'true'
});
//REQUEST
let options = new RequestOptions({headers:this.headers, withCredentials:true});
return this.http.get(this.tasksUrl,options).map(res=>res.json());
Run Code Online (Sandbox Code Playgroud)
代码背面:(Startup.cs)
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder.WithOrigins("http://theURLofTheFront:8080" )
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors("AllowSpecificOrigin");
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
我试试这个:
CORS 预检请求使用 Windows 身份验证返回 HTTP 401。
我添加了自定义标头来指定 IIS 上的“访问控制允许来源”,对我不起作用。
这对我不起作用:https : //blogs.msdn.microsoft.com/friis/2017/11/24/putting-it-all-together-cors-tutorial/
我无法删除默认授权规则。
我提前谢谢你
有几种方法可以实现这一点,可以在这个类似的问题上找到其他答案--> Angular4 ASP.NET Core 1.2 Windows Authentication CORS for PUT and POST Gives 401
可以使用CORS 模块配置 IIS 。
如此处所示: https
: //blogs.iis.net/iisteam/getting-started-with-the-iis-cors-module此处提供更多信息:https : //blogs.iis.net/iisteam/getting-started -with-the-iis-cors-module
IIS CORS 模块旨在在其他 IIS 模块处理相同请求之前处理 CORS 预检请求。OPTIONS 请求始终是匿名的,因此 CORS 模块为 IIS 服务器提供了一种正确响应预检请求的方法,即使需要在服务器上禁用匿名身份验证也是如此。
您需要通过 Webconfig 启用 CORS 模块:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<cors enabled="true">
<add origin="*" allowCredentials="true" />
</cors>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
更精细的控制:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<cors enabled="true">
<add origin="https://readonlyservice.constoso.com" allowCredentials="true">
<allowMethods>
<add method="GET" />
<add method="HEAD" />
</allowMethods>
<allowHeaders>
<add header="content-type" />
<add header="accept" />
</allowHeaders>
</add>
<add origin="https://readwriteservice.constoso.com" allowCredentials="true">
<allowMethods>
<add method="GET" />
<add method="HEAD" />
<add method="POST" />
<add method="PUT" />
<add method="DELETE" />
</allowMethods>
</add>
</cors>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
您可以重定向所有 OPTIONS 请求以始终给出 OK 状态。然而,这将颠覆预检请求的整个想法,因此仅当它适用于您的情况时才使用它。
在 IIS 中安装重定向模块。
将以下重定向添加到您的 Webconfig。
<rewrite>
<rules>
<rule name="CORS Preflight Anonymous Authentication" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^OPTIONS$" />
</conditions>
<action type="CustomResponse" statusCode="200" statusReason="Preflight" statusDescription="Preflight" />
</rule>
</rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)
或者,可以通过在 IIS 中启用匿名身份验证并在 Net Core API 中创建一个中间件来检查一个人是否经过正确身份验证来实现所需的结果。
中间件:
public AuthorizationMiddleware(RequestDelegate next, ILogger logger)
{
_next = next;
_log = logger;
}
public async Task Invoke(HttpContext httpContext)
{
//Allow OPTIONS requests to be anonymous
if (httpContext.Request.Method != "OPTIONS" && !httpContext.User.Identity.IsAuthenticated)
{
httpContext.Response.StatusCode = 401;
await httpContext.Response.WriteAsync("Not Authenticated");
}
await _next(httpContext);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6315 次 |
| 最近记录: |