Request (using authorization in header) to asp.net core API from Angular 4 not working -


i struggling make requests using bearer token angular (4) asp net core api.

here how doing angular:

enter image description here

my api startup.cs code:

public void configureservices(iservicecollection services) {   services.addmvc();   services.addauthentication();    services.addcors(options =>   {     options.addpolicy("corspolicy",         builder => builder.allowanyorigin()         .allowanymethod()         .withheaders("authorization", "accept", "content-type", "origin"));   });    // add configuration singleton here   services.addsingleton<iconfiguration>(configuration);  }  public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory) {   app.usecors("corspolicy");   app.useoptions();   app.use(async (context, next) =>   {     await next();     if (context.response.statuscode == 404 &&        !path.hasextension(context.request.path.value) &&        !context.request.path.value.startswith("/api/"))     {       context.request.path = "/index.html";       await next();     }   });   app.useexceptionhandler(errorapp =>   {     errorapp.run(async context =>     {       context.response.statuscode = 500; // or status accordingly exception type       context.response.contenttype = "application/json";        var error = context.features.get<iexceptionhandlerfeature>();       if (error != null)       {         var ex = error.error;          await context.response.writeasync(ex.message, encoding.utf8);       }     });   });    var jwtauth = new jwtbeareroptions   {     automaticauthenticate = true,     automaticchallenge = true,     authority = $"{configuration["authentication:azuread:aadinstance"]}{configuration["authentication:azuread:tenantid"]}",     audience = configuration["authentication:azuread:clientid"],     tokenvalidationparameters =             new microsoft.identitymodel.tokens.tokenvalidationparameters             {               validissuer = configuration["authentication:azuread:issuer"]             }   };   app.usejwtbearerauthentication(jwtauth);     app.usemvcwithdefaultroute();   app.usedefaultfiles();   app.usestaticfiles(); } 

but keep getting unauthorized error!

enter image description here
enter image description here

i have tried many solutions i've read online, here on stackoverflow (.net core usecors() not add headers, enable options header cors on .net core web api , how disable options request?), couldn't wort! :( when make request using postman, work.

ok ok .. terrible mistake.. can't believe spent hours trying figure out! problem between computer screen , chair.. adding headers authorization in wrong angular service!!!


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -