Server uses Cors but UI still gets allow-origin error










-1














Hello i am trying to issue a http get request to a .NET Core Console App from my Angular 2 frontend and i get the following error:



Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


For me it is curious since i have enabled CORS on the server side as you can see below in the Startup class.



Startup



public class Startup 

public Startup(IConfiguration configuration)
Configuration = configuration;

public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.AddMvc();

public IConfiguration Configuration;
public void Configure(IApplicationBuilder app)
Console.WriteLine("request delievered");
Debug.WriteLine("Entered Server !");
app.UseMvc();
app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );




I make the request from the UI like this:



@Injectable()
export class UserService

private static baseUrl:string="http://127.0.0.1:9300/api";

constructor(private http:HttpClient)


getClientsAsync():Promise<User>
let route=UserService.baseUrl+"/getusers";
var data=(this.http.get(route) //should i have some headers here?
.map(resp=>resp)
.catch(err=>
Observable.throwError(err)

) as Observable<User>).toPromise<User>();

return data;




P.S I have tried with Postman and the request works ,however here in the angular 2 i have not included any headers for my http.get method.Could this be the problem ?










share|improve this question




























    -1














    Hello i am trying to issue a http get request to a .NET Core Console App from my Angular 2 frontend and i get the following error:



    Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


    For me it is curious since i have enabled CORS on the server side as you can see below in the Startup class.



    Startup



    public class Startup 

    public Startup(IConfiguration configuration)
    Configuration = configuration;

    public void ConfigureServices(IServiceCollection services)
    services.AddOptions();
    services.AddMvc();

    public IConfiguration Configuration;
    public void Configure(IApplicationBuilder app)
    Console.WriteLine("request delievered");
    Debug.WriteLine("Entered Server !");
    app.UseMvc();
    app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );




    I make the request from the UI like this:



    @Injectable()
    export class UserService

    private static baseUrl:string="http://127.0.0.1:9300/api";

    constructor(private http:HttpClient)


    getClientsAsync():Promise<User>
    let route=UserService.baseUrl+"/getusers";
    var data=(this.http.get(route) //should i have some headers here?
    .map(resp=>resp)
    .catch(err=>
    Observable.throwError(err)

    ) as Observable<User>).toPromise<User>();

    return data;




    P.S I have tried with Postman and the request works ,however here in the angular 2 i have not included any headers for my http.get method.Could this be the problem ?










    share|improve this question


























      -1












      -1








      -1







      Hello i am trying to issue a http get request to a .NET Core Console App from my Angular 2 frontend and i get the following error:



      Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


      For me it is curious since i have enabled CORS on the server side as you can see below in the Startup class.



      Startup



      public class Startup 

      public Startup(IConfiguration configuration)
      Configuration = configuration;

      public void ConfigureServices(IServiceCollection services)
      services.AddOptions();
      services.AddMvc();

      public IConfiguration Configuration;
      public void Configure(IApplicationBuilder app)
      Console.WriteLine("request delievered");
      Debug.WriteLine("Entered Server !");
      app.UseMvc();
      app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );




      I make the request from the UI like this:



      @Injectable()
      export class UserService

      private static baseUrl:string="http://127.0.0.1:9300/api";

      constructor(private http:HttpClient)


      getClientsAsync():Promise<User>
      let route=UserService.baseUrl+"/getusers";
      var data=(this.http.get(route) //should i have some headers here?
      .map(resp=>resp)
      .catch(err=>
      Observable.throwError(err)

      ) as Observable<User>).toPromise<User>();

      return data;




      P.S I have tried with Postman and the request works ,however here in the angular 2 i have not included any headers for my http.get method.Could this be the problem ?










      share|improve this question















      Hello i am trying to issue a http get request to a .NET Core Console App from my Angular 2 frontend and i get the following error:



      Access to XMLHttpRequest at 'http://127.0.0.1:9300/api/getusers' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


      For me it is curious since i have enabled CORS on the server side as you can see below in the Startup class.



      Startup



      public class Startup 

      public Startup(IConfiguration configuration)
      Configuration = configuration;

      public void ConfigureServices(IServiceCollection services)
      services.AddOptions();
      services.AddMvc();

      public IConfiguration Configuration;
      public void Configure(IApplicationBuilder app)
      Console.WriteLine("request delievered");
      Debug.WriteLine("Entered Server !");
      app.UseMvc();
      app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );




      I make the request from the UI like this:



      @Injectable()
      export class UserService

      private static baseUrl:string="http://127.0.0.1:9300/api";

      constructor(private http:HttpClient)


      getClientsAsync():Promise<User>
      let route=UserService.baseUrl+"/getusers";
      var data=(this.http.get(route) //should i have some headers here?
      .map(resp=>resp)
      .catch(err=>
      Observable.throwError(err)

      ) as Observable<User>).toPromise<User>();

      return data;




      P.S I have tried with Postman and the request works ,however here in the angular 2 i have not included any headers for my http.get method.Could this be the problem ?







      asp.net-core cors






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 8:58









      SET

      7,79843260




      7,79843260










      asked Nov 13 '18 at 8:18









      Bercovici AdrianBercovici Adrian

      1,0801816




      1,0801816






















          1 Answer
          1






          active

          oldest

          votes


















          4














          You need to put UseCors before UseMvc.



           public void Configure(IApplicationBuilder app) 
          Console.WriteLine("request delievered");
          Debug.WriteLine("Entered Server !");
          app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
          app.UseMvc();



          This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276614%2fserver-uses-cors-but-ui-still-gets-allow-origin-error%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            You need to put UseCors before UseMvc.



             public void Configure(IApplicationBuilder app) 
            Console.WriteLine("request delievered");
            Debug.WriteLine("Entered Server !");
            app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
            app.UseMvc();



            This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.






            share|improve this answer

























              4














              You need to put UseCors before UseMvc.



               public void Configure(IApplicationBuilder app) 
              Console.WriteLine("request delievered");
              Debug.WriteLine("Entered Server !");
              app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
              app.UseMvc();



              This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.






              share|improve this answer























                4












                4








                4






                You need to put UseCors before UseMvc.



                 public void Configure(IApplicationBuilder app) 
                Console.WriteLine("request delievered");
                Debug.WriteLine("Entered Server !");
                app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
                app.UseMvc();



                This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.






                share|improve this answer












                You need to put UseCors before UseMvc.



                 public void Configure(IApplicationBuilder app) 
                Console.WriteLine("request delievered");
                Debug.WriteLine("Entered Server !");
                app.UseCors(x => x.AllowAnyHeader(); x.AllowAnyOrigin();x.AllowAnyMethod(); );
                app.UseMvc();



                This is because UseCors adds a middleware (as does UseMvc), and middleware are executed in order from top to bottom. So the request never gets to the CORS middleware.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 8:20









                juunasjuunas

                21.2k34678




                21.2k34678



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276614%2fserver-uses-cors-but-ui-still-gets-allow-origin-error%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党