【发布时间】:2016-09-21 08:00:56
【问题描述】:
我的团队正在 .net 核心中构建一个 web api。我们使用令牌身份验证对任何客户端进行身份验证,令牌来自 Azure AD。
我们把这段代码放在 Startup.cs 中
app.UseJwtBearerAuthentication(options => {
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.Authority = "https://login.windows.net/1da3434a9-e5a6-4e9e-80c3-aca8ed37cb03";
options.Audience = "https://test.onmicrosoft.com/test_api";
options.RequireHttpsMetadata = false;
});
services.AddCors(options =>
{
// Define one or more CORS policies
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
这个只有在我们允许匿名访问时才有效。我检查了令牌,令牌有效。但是每次我们点击控制器时都会抛出 cors 错误。即使我们启用了 cors
【问题讨论】:
-
能给我们看看 CORS 代码吗?
-
@Danny,我更新了我的帖子并添加了 CORS 代码
-
谢谢。你能显示确切的错误信息吗?
-
XMLHttpRequest 无法加载 localhost:44395/api/siteget。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'localhost:44378' 不允许访问。响应的 HTTP 状态代码为 401。
标签: asp.net-core jwt