I have a CORS problem in my ASP .net core MVC application. My Framework version is .net core 3.1
I want to make a request to our Jira Server, so I need to add CORS. My Startup looks like followed:
public void ConfigureServices(IServiceCollection services) {
...
services.AddCors(options =>
{
options.AddPolicy(
"MyPolicy", builder =>
{
builder.WithOrigins("https://jira.server.com").AllowAnyHeader().AllowAnyMethod().AllowCredentials();
});
});
}
public async void Configure(IApplicationBuilder app, IHostingEnvironment env, AuthorizationDbContext context, RoleManager<IdentityRole> roleManager, UserManager<User> userManager)
{
...
app.UseRouting();
app.UseCors("MyPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
}
My xmlHttpRequest looks like this:
var data = { "fields": { "project": { "key": "projectName" }, "summary": "Test", "description": "Test", "issuetype": { "name": "Bug" }, "fixVersions": [{ "name": "Ideenpool" }] } }
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
};
xhr.open("POST", "https://jira.server.com/api/2/issue", true);
xhr.withCredentials = true;
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Access-Control-Allow-Origin", "https://jira.server.com");
xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
xhr.send(data);
I alway get the error: Access to XMLHttpRequest at 'https://jira.server.com/api/2/issue' from origin 'https://localhost:44301' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I tried to allow all Origins and put a "*" in my request, but always the same error. My curl request in cmd works.
Has anyone an Idea? What am I missing?
Best regards Sebastian