I have a .net core webapi project set up to accept cross origin requests like so
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(opts => opts
.WithOrigins("https://fiddle.jshell.net")
.AllowCredentials()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseHttpsRedirection();
app.UseMvc();
}
This has a values controller with a GET method like so
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return Ok("cookies: " + string.Join(", ", HttpContext.Request.Cookies.Select(x => x.Key)));
}
}
Now I am trying to send a fetch request from the browser like so
fetch('https://api.domain.com/api/values', {
headers:
{
'Content-Type': 'application/json'
},
credentials: 'include',
mode: 'cors'
})
.then(function(resp){
resp.text().then(function(data) {
console.log(data);
})
})
.catch(function(err){
console.log(err)
});
But this doesn't send the cookies from the page to the api. What am I missing here? I have tried all the solutions I could find about this including turning off third party cookies
Update
So, I still don't have an answer to why this doesn't work or any authoritative sources saying you can't send cookies cross-domain (or even one that says cross-origin != cross-domain).
What we found is that even cookies from sub-a.domian.com
would not be sent to sub-b.domain.com
. The way we 'solved' this is to create a cookie that is permanently bound to domain.com
because those cookies are sent to sub-a.domain.com
and sub-b.domain.com
.