【发布时间】:2020-01-06 13:37:26
【问题描述】:
在我写这篇文章之前,我花了很多时间搜索并尝试了很多东西,但没有任何效果。这是一个 .NetCore 2.2 Web Api,我从 Angular 8 应用程序中使用它。
如果我从同一个域调用 web api,它可以正常工作。
如果我从其他域(或本地主机)调用 Web api,它会返回 401(未授权)。
如果我在禁用 Windows 身份验证的情况下从其他域(或本地主机)调用 Web api - 它工作正常(因此正确配置了 CORS)。
在我的 launchSettings.json 我有:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
在我的 web.config 我有:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" arguments="%LAUNCHER_ARGS%" />
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
</configuration>
在 IIS 我启用了 Windows 身份验证并禁用了匿名身份验证。
在我的 Startup.cs 我有:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
和
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
app.UseCors("CorsPolicy");
app.UseMvc();
}
我错过了什么?对此有何想法?
【问题讨论】:
标签: angular asp.net-core .net-core cors windows-authentication