In a nutshell, I have a web api and implemented Thinktecture.IdentityModel for CORS support since my api get called from mobile apps.
I'm not developing the mobile apps and for demonstration purposes created a standalone html/jquery project calling the various functions in my api.
However when I run this on my local machine, the thing fail. Move it to a server and it works fine. After searching I realized this is because the above tool would not allow calls from "localhost", even though I set it to it's most relaxed setting (that
I know off)
How or why is it failing? If the client is on the same localhost as the wervice then there's no CORS involved, so maybe it's failing for other reasons?
Ok, so this is a little different. This is a local html file, yes? And then it's simply calling some Ajax endpoint, yes?
So it's coming back with 200 OK, right? And you don't expect that? Think of this client as a native C# client -- there's no CORS in those either. CORS only comes into play when a page loaded from one origin (not a file based page) tries to call another origin.
krokonoster
Contributor
4291 Points
1352 Posts
Thinktecture.IdentityModel + Localhost
Nov 21, 2012 08:17 PM|LINK
Hi,
I asked this before but worked around it.
In a nutshell, I have a web api and implemented Thinktecture.IdentityModel for CORS support since my api get called from mobile apps.
I'm not developing the mobile apps and for demonstration purposes created a standalone html/jquery project calling the various functions in my api.
However when I run this on my local machine, the thing fail. Move it to a server and it works fine. After searching I realized this is because the above tool would not allow calls from "localhost", even though I set it to it's most relaxed setting (that I know off)
private void RegisterCors(MvcCorsConfiguration corsConfig) { // corsConfig.AllowAll(); corsConfig.ForAllOrigins().AllowAllMethodsAndAllRequestHeaders(); }Anyone know if it's possible and how to allow calls from localhost?
BrockAllen
All-Star
27434 Points
4891 Posts
MVP
Re: Thinktecture.IdentityModel + Localhost
Nov 22, 2012 02:30 PM|LINK
How or why is it failing? If the client is on the same localhost as the wervice then there's no CORS involved, so maybe it's failing for other reasons?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
krokonoster
Contributor
4291 Points
1352 Posts
Re: Thinktecture.IdentityModel + Localhost
Nov 22, 2012 02:38 PM|LINK
If there is no CORS involved (same machine) it's fine.
If the API is called from a client (say mobile phone), it's fine.
But if I create an html file, use jquery to call the api, and run it from my local machine, calling the api on the server, it fail.
I'm not comfortable making the api details public, but I could mail you a html file described as above and you could see for yourself?
BrockAllen
All-Star
27434 Points
4891 Posts
MVP
Re: Thinktecture.IdentityModel + Localhost
Nov 22, 2012 03:01 PM|LINK
Are you using Chrome? Chroms doesn't allow CORS at all from a local file.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
krokonoster
Contributor
4291 Points
1352 Posts
Re: Thinktecture.IdentityModel + Localhost
Nov 22, 2012 03:38 PM|LINK
Fail in all browsers (200 OK???) but when using Fiddler (though I know next to nothing about Fiddler) it return "True" as it should.
Using a very simple method on my api (it's not asp.net web api, but an asp.net mvc 4 controller action)
public class HelperController : Controller { public JsonResult Ping() { return Json(true, JsonRequestBehavior.AllowGet); } }Maybe I call it wrong in my 'demo' aka 'sandbox'?
<!doctype html> <html> <head> <title>Demo Api Client</title> <meta charset="utf-8" /> </head> <body> <a id="btnPing" href="#">Ping API</a> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"> </script> <script type="text/javascript"> $(document).ready(function () { $('#btnPing').live('click', function (event) { var url = 'http://www.mysite.co.za/api2/ping'; $.ajax({ url: url, dataType: 'json', success: function (data) { alert("The Server is Online"); }, error: function (error) { alert("The Server seems to be Offline"); } }); event.preventDefault(); }); }); </script> </body> </html>pm you the actual url if that might help in any way.
BrockAllen
All-Star
27434 Points
4891 Posts
MVP
Re: Thinktecture.IdentityModel + Localhost
Nov 22, 2012 03:45 PM|LINK
Ok, so this is a little different. This is a local html file, yes? And then it's simply calling some Ajax endpoint, yes?
So it's coming back with 200 OK, right? And you don't expect that? Think of this client as a native C# client -- there's no CORS in those either. CORS only comes into play when a page loaded from one origin (not a file based page) tries to call another origin.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
krokonoster
Contributor
4291 Points
1352 Posts
Re: Thinktecture.IdentityModel + Localhost
Nov 22, 2012 03:51 PM|LINK
Sorry, I know you must be fed up of me not getting it. (Other than this has nothing to do with CORS)
I uploaded the html file as is to the server on which the api is used. Nothing changed.
Calling that have the "api" return me the "true" I return in the action method.
Really, this make no sense to me, I apologize but I'm totally stumped.