title: No 'Access-Control-Allow-Origin' header is present on the requested resource Asp.Net Web Api Owin and Angular 8
body: <p>I have a Asp.Net Web Api with JWT Oauth authentication mechanism. So in <code>Startup.cs</code> file I defined cors policy like this:</p>
  
  <pre><code>app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
  </code></pre>
  
  <p>And I use validate ticket like this at <code>CustomOAuthProvider</code> class:</p>
  
  <pre><code>public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
  {
  
      var allowedOrigin = "*";
  
      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
  
      var userManager = context.OwinContext.GetUserManager&lt;ApplicationUserManager&gt;();
  
      ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
  
      if (user == null)
      {
          context.SetError("invalid_grant", "The user name or password is incorrect.");
          return;
      }
  
      if (!user.EmailConfirmed)
      {
          context.SetError("invalid_grant", "User did not confirm email.");
          return;
      }
  
  
      ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
  
      var ticket = new AuthenticationTicket(oAuthIdentity, null);
  
      context.Validated(ticket);
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>As you can see I defined for response headers in this:</p>
  
  <pre><code>var allowedOrigin = "*";
  
      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
  </code></pre>
  
  <p>I couldnt set anywhere <code>EnableCors</code>, just in <code>StartUp.cs</code>.</p>
  
  <p>But I am getting this error in client side(Angular 8):</p>
  
  <pre><code>Access to XMLHttpRequest at 'http://localhost:50522/oauth/token' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  </code></pre>
  
  <p>I am trying to use <code>login</code> method in angular like this:</p>
  
  <pre><code>login(username: string, password: string, grant_type: string) {
      let headers = new HttpHeaders();
      headers.append('Content-Type', 'application/json');
      return this.http.post&lt;any&gt;(`${environment.urlAddress}/oauth/token`, JSON.stringify({ username, password, grant_type }), { headers })
          .pipe(map(user =&gt; {
              console.log(user);
              // store user details and jwt token in local storage to keep user logged in between page refreshes
              console.log(user);
              localStorage.setItem('currentUser', JSON.stringify(user));
              this.currentUserSubject.next(user);
              return user;
          }))
  }
  </code></pre>
  
  <p>I couldn't understand what is the problem exactly.</p>
  
  <p>Please help.</p>