title: Pass JWT token from angular HttpClient to access secure .NET Core Web API
body: <p>I need to call secure Web API from Angular 9 application by presenting the token. I am using Angular with .NET CORE 3.1 Web API. I have managed to generate Azure B2C token but stuck to call secure web api as I got CORS error.</p>
  <h2>Angular component calling Web API end-point</h2>
  <pre><code>testAPI1(){
      console.log(&quot;calling test API ...&quot;);
  
      const myheaders = new HttpHeaders({
          'Content-Type': 'application/json; charset=utf-8',
          'Authorization': this.authService.accessToken
      });
  
      this.http.get('https://localhost:5001/txn/v1/Dashboard/GetMessage', {headers: myheaders})
          .subscribe((data)=&gt;{
              console.warn(data);
          })
  }
  </code></pre>
  <h2>Auth Service</h2>
  <pre><code>@Injectable()
  export class AuthService implements OnInit{
  
  constructor(
      private oauthService: OAuthService,
      private router: Router
  
  ){// other code}
  
  
  public get accessToken() { 
      return this.oauthService.getAccessToken(); 
  }
  </code></pre>
  <h2>Web API controller &amp; endpoint</h2>
  <pre><code>[Authorize]
  [Route(&quot;txn/v1/[controller]/[action]&quot;)]
  [EnableCors(&quot;CorsPolicy&quot;)]
  [ApiController]
  public class DashboardController : ControllerBase
  {
      [HttpGet]
      public ActionResult&lt;HelloMessage&gt; GetMessage()
      {
          var result = new HelloMessage()
          {
              GivenName = &quot;james&quot;,
              ReturnMessage = &quot;Dashboard@ Hello, Welcome to Digital tech&quot;
          };
          return result;
      }
  </code></pre>
  <h2>Startup.cs</h2>
  <pre><code>public void ConfigureServices(IServiceCollection services)
      {
         //JWT Authentication 
          services.AddAuthentication(options =&gt;
          {
              options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
              options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
          }).AddJwtBearer(jwtConfig =&gt; 
              {
                  jwtConfig.Audience = Configuration[&quot;AzureAdB2C:ResourceId&quot;];
                  jwtConfig.Authority = $&quot;{Configuration[&quot;AzureAdB2C:Instance&quot;]}{Configuration[&quot;AzureAdB2C:TanantId&quot;]}&quot;;
                  jwtConfig.RequireHttpsMetadata = false;
                  jwtConfig.SaveToken = true;
                  jwtConfig.TokenValidationParameters = new TokenValidationParameters
                  {
                      ValidateIssuerSigningKey = true,
                      ValidateIssuer =true,
                      ValidateAudience = true,
                      ValidateLifetime = true
  
                  };
          });
  
   //CORS policy
   services.AddCors(options =&gt;
                            options.AddPolicy(&quot;CorsPolicy&quot;, builder =&gt; builder.AllowAnyOrigin()));
  </code></pre>
  <h2>error</h2>
  <p><a href="https://i.stack.imgur.com/yxSRQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yxSRQ.png" alt="enter image description here" /></a></p>