0

I've several projects that one of them is for Authentication. However I've set CORS configuration, it still returns errors.

Note: Web Api apps work with postman.

Here is what I've done so far:

Startup.cs

[assembly: OwinStartup(typeof(LabelGenerator.Startup))]
namespace LabelGenerator {
    public partial class Startup {
        public void Configuration(IAppBuilder app) {
            ConfigOAuth(app);
            ConfigWebApi(app);
        }
    }
}

Startup.Auth.cs

namespace LabelGenerator {
    public partial class Startup {
        private void ConfigOAuth(IAppBuilder app) {
            OAuthBearerAuthenticationOptions oAuthServerOptions = new OAuthBearerAuthenticationOptions();
            // Token Generation
            app.UseOAuthBearerAuthentication(oAuthServerOptions);
        }

        private void ConfigWebApi(IAppBuilder app) {
            HttpConfiguration conf = new HttpConfiguration();
            WebApiConfig.Register(conf);
            app.UseWebApi(conf);

            // CORS
            app.UseCors(CorsOptions.AllowAll);
        }
    }
}

Web.config

<modules runAllManagedModulesForAllRequests="true">
  <remove name="ApplicationInsightsWebTracking" />
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>

1 Answer 1

1

According to this answer I changed this :

private void ConfigWebApi(IAppBuilder app) {
    HttpConfiguration conf = new HttpConfiguration();
    WebApiConfig.Register(conf);
    app.UseWebApi(conf);

    // CORS
    app.UseCors(CorsOptions.AllowAll);
}

to this :

private void ConfigWebApi(IAppBuilder app) {
    HttpConfiguration conf = new HttpConfiguration();
    WebApiConfig.Register(conf);

    // CORS
    app.UseCors(CorsOptions.AllowAll);
    app.UseWebApi(conf);
}

And now CORS is working properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.