Search code examples
angularasp.net-web-apicorsowin-middleware

Owin token call from angular 4 No 'Access-Control-Allow-Origin' header is present on the requested resource


I am using owin to generate oAuth token in asp.net webapi and I have added the UserCors as the 1st line in Configuration in Statrup.cs

public void Configuration(IAppBuilder app)
{
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    ConfigureOAuth(app);
}

I also added it again inside ConfigureOAuth(app);

I manually tried to add header inside GrantResourceOwnerCredentials like below

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

But didn't works. I keep getting the error No 'Access-Control-Allow-Origin' header is present on the requested resource from angular side.

When I add the headers in the config file it says duplicate.

<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT,DELETE" />
</customHeaders>

Not sure what's going on.


Solution

  • The trick is to delete the Global.asax and call the WebApiConfig.Register from the startup file. This link helped me.