C# 在访问控制允许源标题中找不到源[域]。ASP.net核心API mvc
尝试从asp.net CORE mvc网站调用asp.net CORE webAPI时,我总是得到: 在访问控制允许源标题中找不到源[域] 主站点url类似:C# 在访问控制允许源标题中找不到源[域]。ASP.net核心API mvc,c#,asp.net-core,C#,Asp.net Core,尝试从asp.net CORE mvc网站调用asp.net CORE webAPI时,我总是得到: 在访问控制允许源标题中找不到源[域] 主站点url类似:https://mysite/Login/API_Request Api url类似:https://auth.mysite/api/values 当我直接输入url时,Api运行良好,但ajax请求不起作用: 请求: $(document).ready(function () { getIdentity(); }); function
https://mysite/Login/API_Request
Api url类似:
https://auth.mysite/api/values
当我直接输入url时,Api运行良好,但ajax请求不起作用:
请求:
$(document).ready(function () {
getIdentity();
});
function getIdentity() {
$.ajax({
type: "GET",
data: "",
url: "https://auth.mysite/api/values",
dataType: 'json',
xhrFields: {
withCredentials: true
},
contentType: false,
processData: false,
success: function (response) {
response = response.replace(/\"/g, "");
console.log(response);
},
error: function (response) {
console.log(response.statusText);
}
});
}
API控制器
[Authorize]
[Route("api/[controller]")]
[EnableCors("AllowAll")]
public class ValuesController : Controller
{
[HttpGet]
public ActionResult Get()
{
var u = User;
WindowsIdentity identity = null;
if (HttpContext== null)
{
identity = WindowsIdentity.GetCurrent();
}
else
{
identity = HttpContext.User.Identity as WindowsIdentity;
}
return Json(identity.User.Value);
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => {
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials();
});
});
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("AllowAll");
app.UseMvc();
}
我尝试使用Microsoft.AspNetCore.Cors,但未能成功:
startup.cs
[Authorize]
[Route("api/[controller]")]
[EnableCors("AllowAll")]
public class ValuesController : Controller
{
[HttpGet]
public ActionResult Get()
{
var u = User;
WindowsIdentity identity = null;
if (HttpContext== null)
{
identity = WindowsIdentity.GetCurrent();
}
else
{
identity = HttpContext.User.Identity as WindowsIdentity;
}
return Json(identity.User.Value);
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => {
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials();
});
});
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("AllowAll");
app.UseMvc();
}
上面的代码似乎可以工作,我只是再次发布了API和主站点,一切正常。AllowCredentials似乎是必需的,我认为这是因为API使用Windows身份验证,而主站点不使用。我对API statup上的授权做了一些更改:
services.AddCors(options => {
options.AddPolicy("AllowAll",
builder =>
{
builder.WithOrigins("http://mysite").WithMethods("GET").AllowAnyHeader().AllowCredentials();
});
});
出于某些安全原因。我明白了,我的问题很愚蠢,但不管怎样:您是否在startup.cs for API中配置了Cors,而不是在网站中?另一个问题是:请求中确实需要凭据吗?CORS规范规定,如果存在访问控制允许凭据标头,则将源代码设置为“*”(所有源代码)无效。@d\f请求代码在主站点上,但APIController和startup.cs在API上。凭证似乎确实是必要的,我认为这是因为API使用Windows身份验证。我刚刚重新发布了主站点和API,现在它似乎可以用上面相同的代码正常工作。