title: Access to XMLHttpRequest has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
body: <p>I have come across a situation where I make a call from my angular 7 application to my asp.net web api application. When I call an HTTP post method in angular to the application running on a different port it throws an exception.</p>
  
  <p>Access to XMLHttpRequest at '<a href="http://localhost/oauthservice/api/token" rel="nofollow noreferrer">http://localhost/oauthservice/api/token</a>' from origin '<a href="http://localhost:4200" rel="nofollow noreferrer">http://localhost:4200</a>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.</p>
  
  <p>My Angular application Running on '<a href="http://localhost:4200" rel="nofollow noreferrer">http://localhost:4200</a>' port number:4200 My asp.net web api application running on '<a href="http://localhost/oauthservice/api/token" rel="nofollow noreferrer">http://localhost/oauthservice/api/token</a>': port number:80 Microsoft IIS .</p>
  
  <p>if you have a time please help me.</p>
  
  <p>                                        </p>
  
  <p>sign-in.component.ts</p>
  
  <pre><code>OnSubmit(userName: any, password: any) {
   this.userService.userAuthentication(userName, password).subscribe((data: any) =&gt; {
     localStorage.setItem('userToken', JSON.stringify(data));
     this.router.navigate(['/home']);
   },
   (err: HttpErrorResponse) =&gt; {
       this.isLoginError = true;
     });
  }
  </code></pre>
  
  <p>user.service.ts</p>
  
  <pre><code>userAuthentication(userName, password) {
  const data = 'grant_type=password&amp;username=' + userName + '&amp;password=' + password;
  // let reqHeader = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded', 'No-Auth': 'True' });
   // tslint:disable-next-line:max-line-length
   return this.http.post(this.apiUrl + 'token', data, { headers: {'Content-Type': 'application/x-www-form-urlencoded'}});
    }
  </code></pre>
  
  <p>form server side </p>
  
  <p>startup.cs</p>
  
  <pre><code>public void ConfigureOAuth(IAppBuilder app, HttpConfiguration config)
          {
              OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
              OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()
              {
  
                  TokenEndpointPath = new PathString("/api/token"),
                  AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
                  Provider = new AuthorizationServerProvider((IAccountsRepository)config.DependencyResolver.GetService(typeof(IAccountsRepository))),
                  RefreshTokenProvider = new RefreshTokenProvider(),
                  AllowInsecureHttp = true,
              };
              //Token Generation
              app.UseOAuthAuthorizationServer(oAuthServerOptions);
              app.UseOAuthBearerAuthentication(OAuthBearerOptions);
          }
  </code></pre>
  
  <p>AuthorizationServerProvider.cs</p>
  
  <pre><code> public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
      {
          var identity = new ClaimsIdentity(context.Options.AuthenticationType);
  
          //Authenticate the user credentials
          if (_accounts.Login(context.UserName, context.Password))
          {
              t_userDetail user = await _accounts.GetUserDetails(context.UserName);
  
              identity.AddClaim(new Claim(ClaimTypes.Role, _accounts.GetUserRole(context.UserName)));
              identity.AddClaim(new Claim("username", context.UserName));
              identity.AddClaim(new Claim("fullName", user.firstName + " " + user.lastName));
              identity.AddClaim(new Claim("Email", user.email));
              identity.AddClaim(new Claim("Phone", user.phone));
              identity.AddClaim(new Claim("role", _accounts.GetUserRole(context.UserName)));
              identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
              identity.AddClaim(new Claim("LoggedOn",DateTime.Now.ToString()));
  
              var props = new AuthenticationProperties(new Dictionary&lt;string, string&gt;
              {
                  {
                      "as:client_id", context.ClientId ?? string.Empty
                  },
                  {
                      "userName", context.UserName
                  },
                  {
                      "fullName",user.firstName + " "  + user.lastName
                  },
                  {
                      "Email", user.email
                  },
                  {
                      "role", _accounts.GetUserRole(context.UserName)
                  },
                  {
                      "Phone", user.phone
                  },
  
              });
              var ticket = new AuthenticationTicket(identity, props);
              context.Validated(ticket);
          }
          else
          {
              context.SetError("invalid_grant", "Provided username or password is incorrect");
          }
      }
  </code></pre>