1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

.net core 8 Web API CORS problems with Angular18 frontend

Discussão em 'Angular' iniciado por Broken, Setembro 13, 2024.

  1. Broken

    Broken Guest

    I got a CORS problem I simply can't get my head around, hope someone can help me. Frontend is Angular 18. My backend .net core 8 Web API got a few endpoints, where not all works for some reason. Everything works as I expect on localhost, login etc. is butter smooth. But as soon as I publish the web api, only some of my endpoints works.


    Access to fetch at 'https://api.brokenholiday.online/authenticate/login'
    from origin 'https://tools.brokenholiday.online'
    has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
    If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled


    My Program.cs

    builder.Services.AddScoped<JwtSecurityTokenHandlerWrapper>();
    builder.Services.AddControllersWithViews(options => {
    options.Filters.Add(typeof(JwtAuthorizeFilter));
    });

    builder.Services.AddCors(options => {
    options.AddPolicy("AllowSpecificOrigin",
    builder => builder
    .WithOrigins("https://*.brokenholiday.online", "http://localhost:4200")
    .SetIsOriginAllowedToAllowWildcardSubdomains()
    .AllowAnyHeader()
    .AllowAnyMethod()
    .AllowCredentials());
    });
    builder.Services.AddControllers();
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();

    var app = builder.Build();
    app.UseMiddleware<JwtMiddleware>();
    app.UseCors("AllowSpecificOrigin");
    app.MapControllers();
    app.UseHttpsRedirection();
    app.UseAuthentication();
    app.UseAuthorization();
    app.Run();

    Continue reading...

Compartilhe esta Página