c# - Asp.net WebAPI enable Cors to specific SSL/https origin -


below part of web api project.

public class triviacontroller : apicontroller {             public httpresponsemessage get()     {         questionanswer _questionanswer = new questionanswer();         triviaquestion _triviaquestion = _questionanswer.getquestion("test@yahoo.com");         return request.createresponse(httpstatuscode.ok, _triviaquestion);     }     }  public static class webapiconfig {     public static void register(httpconfiguration config)     {         enablecrosssiterequests(config);         addroutes(config);     }      private static void addroutes(httpconfiguration config)     {         config.routes.maphttproute(             name: "default",             routetemplate: "api/{controller}/{id}",             defaults: new { id = routeparameter.optional }         );     }      private static void enablecrosssiterequests(httpconfiguration config)     {         var cors = new enablecorsattribute(             origins: "*",             headers: "*",             methods: "*");         config.enablecors(cors);     } } 

my client application receive json data returned contoller. working correct until modify specific origins below.

var cors = new enablecorsattribute(             origins: "https://jsfiddle.net/", //"https://localhost:44304/",             headers: "*",             methods: "*"); 

after that, client application receive null value , error message saying status code 0.
according this reference link, know because of cors.

below client code. simple.

alert("*"); $.getjson("https://localhost:44300/api/trivia", function(data) {   alert(json.stringify(data)); }) .done(function() { alert('getjson request succeeded!'); }) .fail(function(jqxhr, textstatus, errorthrown) {  alert('getjson request failed! ' + textstatus + ' :: ' + jqxhr + ' :: ' + errorthrown);   $.each(jqxhr, function(k, v) {     //display key , value pair     alert(k + ' ' + v); });  }) .always(function() { alert('getjson request ended!'); }); 

to see on jsfiddle, click here.

so question is,
possible set allow origin using ssl? if doning wrong?

try adding origins separately. use developer tools in browser make sure origins match intend them be.

var cors = new enablecorsattribute("https://localhost:portno", "*", "*"); // local cors.origins.add("https://jsfiddle.net");  // jsfiddle  config.enablecors(cors); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -