mattbrailsford / umbraco-authu

An Umbraco plugin to add an OAuth API endpoint to allow authenticated Members/Users via OAuth
MIT License
71 stars 22 forks source link

Error 405: The requested resource does not support http method OPTIONS #12

Closed biapar closed 7 years ago

biapar commented 7 years ago

Hi,

if you call a method like your example, we receive the error "The requested resource does not support http method OPTIONS". Why?

`[Our.Umbraco.AuthU.Web.WebApi.OAuth("realmusers")] public class LGIWFPraticheController : UmbracoApiController {

    [HttpGet]
    [Authorize]
    public string HelloWorld()
    {
        return "Hello " + Members.GetCurrentMember()?.Name;
    }

}`

called by: ` var token = localStorage.getItem("token");

var headers = new Headers();
headers.append('authorization', 'Bearer ' + token);

let options = new RequestOptions({ headers: headers });

let body = JSON.parse('{}');

return this.http.get('http://localhost:60936/umbraco/api/LGIWFPratiche/helloworld', options) .map(this.handleData) .catch(this.handleError);`

biapar commented 7 years ago

Hi, the problem is server side. How solve?

mattbrailsford commented 7 years ago

Sounds like you need to enable CORS, take a look here https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api#enable-cors

Ultimately, you should be able to call this in an application starting event handler:

GlobalConfiguration.Configuration.EnableCors();

And then on your API controller, you'll need to add the endable CORS attribute:

[EnableCors("*", "*", "*")]

You'll need to make sure the params match your configuration though. See the link for details on what those params are.

biapar commented 7 years ago

I follow this info by Casper

https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/76788-how-do-i-get-around-the-cross-domain-problem-using-the-umbraco-web-api-using-post#comment-245424