Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème AddCors / AddPolicy

Sujet résolu
    17 septembre 2020 à 14:34:01

    Bonjour

    Voilà j'ai suivi un tuto pour faire une page web mélangeant React.js et C#

    Sur le react.js tout va bien 

    Par contre j'ai un soucis avec le C# 

    (voilà le lien du tuto : https://www.red-gate.com/simple-talk/dotnet/net-development/creating-asp-net-apps-with-react/)

    voilà l'erreur que j'ai lorsque j'execute mon programme : 

    "The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the CORS policy by listing individual origins if credentials needs to be supported."

    J'ai chercher sur internet de pourquoi j'avais ça et comment le fix mais j'ai pas trop compris.

    Du coup voici mon code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Focus_.Services;
    
    namespace Focus_
    {
        public class Startup
        {
            // This method gets called by the runtime. Use this method to add services to the container.
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddSingleton<UserService>();
                services.AddCors(c => c.AddPolicy("ReactPolicy", builder => builder.AllowAnyOrigin()
                         .AllowAnyMethod()
                         .AllowAnyHeader()
                         .AllowCredentials()
                         ));
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseCors("ReactPolicy");
    
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseRouting();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapGet("/", async context =>
                    {
                        await context.Response.WriteAsync("Hello World!");
                    });
                });
            }
        }
    }

    Si quelqu'un peut m'expliquer et m'aider.

    Merci d'avance.

    • Partager sur Facebook
    • Partager sur Twitter
      24 septembre 2020 à 11:45:15

      Le message est explicite: il ne peut pas y avoir .AllowAnyOrigin() et .AllowCredentials() en même temps; donc soit tu supprimes .AllowCredentials(), soit il faut définir les difféntes Origins

      Une autre solution pour autoriser les 2, c'est de remplacer .AllowAnyOrigin() par .SetIsOriginAllowed(origin => true)

      (=> https://jasonwatmore.com/post/2020/05/20/aspnet-core-api-allow-cors-requests-from-any-origin-and-with-credentials)

      • Partager sur Facebook
      • Partager sur Twitter
        14 octobre 2020 à 11:54:30 - Message modéré pour le motif suivant : Réponse à un message supprimé


        Je recherche un CDI/CDD/mission freelance comme Architecte Logiciel/ Expert Technique sur technologies Microsoft.

        Problème AddCors / AddPolicy

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown