CORS is not working in web api with OWIN authentication

前端 未结 5 589
花落未央
花落未央 2020-12-05 00:36

In my application i am using web api with token based authentication with CORS support, but when client request for the token, an error occured due to CORS (Cross-Origin Re

相关标签:
5条回答
  • 2020-12-05 01:16

    OWIN and Microsoft.AspNet.WebApi.Cors are two separate libraries and each one needs separate configuration.

    Disable use of CORS with OWIN:

    public void Configuration(IAppBuilder app)
    {
            //app.UseCors(CorsOptions.AllowAll);
    

    Find GrantResourceOwnerCredentials method and add Access-Control-Allow-Origin to context so when it returns a call after authentication is completed that browser finds the header and accepts it.

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "http://localhost" });
    

    Now install Microsoft.AspNet.WebApi.Cors package from Nuget to your webapi project, and add this to Register method

    public static void Register(HttpConfiguration config)
    {
            var cors = new EnableCorsAttribute("http://localhost, ", "accept,accesstoken,authorization,cache-control,pragma,content-type,origin", "GET,PUT,POST,DELETE,TRACE,HEAD,OPTIONS");
    
            config.EnableCors(cors);
    

    This did it for me.

    0 讨论(0)
  • 2020-12-05 01:23

    Had the same problem. In addition to the above indications (using app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll) only, and setting it up as first thing), I had to specify the following in the application Web.config file to be able to handle Option Requests:

    <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
      <remove name="OPTIONSVerbHandler"/>
      <remove name="TRACEVerbHandler"/>          
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
    </handlers>
    </system.webServer>
    

    Due to some headers that I was sending in the authentication request, an Options request is sent before the actual POST request, and it needs to return the correct 'Access-Control-Allow-Origin' header before the POST is sent.

    If none of the CORS headers are returned by the options response, then the POST will not be sent at all. The added configuration enables this behaviour as well as for Trace.

    As explained in this post

    0 讨论(0)
  • 2020-12-05 01:24

    I had this similar problem, I tried all the options above in startup.cs i added app.UseCors(CorsOptions.AllowAll); at the top and in the WebApiConfig i disabled

    public static void Register(HttpConfiguration config)
    {
        //var cors = new EnableCorsAttribute("*", "*", "*");
        //config.EnableCors(cors);
    }
    

    and also disabled cors in

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
    }
    

    And repeated this in reverse order and also included entries in web.config but all did not work.

    When I begun asking myself why is app.UseWebApi(config); not accessible yet I have seen it work somewhere. I looked around and found out installing Install-Package Microsoft.AspNet.WebApi.OwinSelfHost fixes it.

    Eventually, it fixed the whole problem, though app.UseCors(CorsOptions.AllowAll) has to be placed first in the startup.cs method. In fact without app.UseWebApi(config), I tested in postman and the end points actually didn't exist. Overall it's working pretty well for me now.

    0 讨论(0)
  • 2020-12-05 01:33

    Especially if you are having problem with the Web API bearer token when using CORS then dont forget to put "TOKEN" in the list of your allowed methods.

    Please put the code in your system.webServer of web.config, that is how i solved mine

    <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE, TOKEN" />
     </customHeaders>
    
    0 讨论(0)
  • 2020-12-05 01:39

    Be sure you've got only

    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

    configured, and not also the old style 'config.EnableCors()' in your Global.asax or WebApiConfig. Furthermore: place the above statement as the first one in your owin Startup class. Yes that really makes a difference, setting it later can also cause cors to not work.

    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    
            ... etc
    
    0 讨论(0)
提交回复
热议问题