c# - Getting CORS to work with Web API and SignalR -


been having difficult getting signalr , cors working asp.net web api v2 using owin authentication.

it seems though can can either cors working signalr or web api not both. configuring cors via code trying add headers via web.config did not work me. below configuration method startup.cs

    public void configuration(iappbuilder app)     {         app.usecors(corsoptions.allowall);         httpconfiguration httpconfig = new httpconfiguration();         configureoauthtokengeneration(app);         configureoauthtokenconsumption(app);         configurewebapi(httpconfig);         app.usewebapi(httpconfig);     } 

with above configuration the web api works. able authenticate , call api methods client. however, error when client attempts reach signalr scripts. notice cors error no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:8080' therefore not allowed access. in second error message.

http://localhost:49834/signalr/negotiate?clientprotocol=1.5&connectiondata=%5b%7b%22name%22%3a%22loginhub%22%7d%5d&_=1461093918311 failed load resource: server responded status of 404 (not found)  xmlhttprequest cannot load http://localhost:49834/signalr/negotiate?clientprotocol=1.5&connectiondata=%5b%7b%22name%22%3a%22myhub%22%7d%5d&_=1461093918311. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:8080' therefore not allowed access. response had http status code 404. 

this confusing ... why doesn't cors configuration cover path signalr scripts located @ http://localhost:8080/signalr/?

so after searching , googling while came across so post dealt signalr. answer suggested added in following configuration method. after signalr worked!

public void configuration(iappbuilder app) {     app.usecors(corsoptions.allowall);      // ****** start added code ******     //branch pipeline here requests start "/signalr"     app.map("/signalr", map =>     {         // setup cors middleware run before signalr.         // default allow origins. can          // configure set of origins and/or http verbs         // providing cors options different policy.         map.usecors(corsoptions.allowall);         var hubconfiguration = new hubconfiguration         {             // can enable jsonp uncommenting line below.             // jsonp requests insecure older browsers (and             // versions of ie) require jsonp work cross domain             // enablejsonp = true;         };         // run signalr pipeline. we're not using mapsignalr         // since branch runs under "/signalr"         // path.         map.runsignalr(hubconfiguration);     });     // ****** end added code *******      httpconfiguration httpconfig = new httpconfiguration();     configureoauthtokengeneration(app);     configureoauthtokenconsumption(app);     configurewebapi(httpconfig);     app.usewebapi(httpconfig); } 

why second section configuration needed? in order signalr work? shouldn't have app.usecors(corsoptions.allowall); worked?


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -