简体   繁体   English

在Azure移动服务中启用CORS-未授权的选项

[英]Enable CORS in Azure Mobile Services - OPTIONS not authorized

I am creating .Net web services using Azure Mobile Services. 我正在使用Azure移动服务创建.Net Web服务。 The services themselves work fine, but I want to enable CORS. 服务本身可以正常工作,但是我想启用CORS。

My Global.asax contains: 我的Global.asax包含:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    if (HttpContext.Current.Request.HttpMethod != "OPTIONS") return;
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
        "Authorization, Origin, Content-Type, Accept, X-Requested-With,x-zumo-application,x-zumo-installation-id");
    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
    HttpContext.Current.Response.End();
}

My WebAPIConfig.cs contains: 我的WebAPIConfig.cs包含:

    public static void Register()
    {
        ConfigOptions options = new ConfigOptions();
        HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));

        var cors = new EnableCorsAttribute("*", "*", "*","*");
        config.EnableCors(cors);

        config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
    }

My request/response: 我的要求/回应:

OPTIONS http://********.azure-mobile.net/API/MyLogin?username=username&password=password&email=testtest%40example.com&_=140191793307 HTTP/1.1
Host: ********.azure-mobile.net
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: null
Access-Control-Request-Method: GET
Access-Control-Request-Headers: content-type,x-zumo-application,x-zumo-installation-id
Connection: keep-alive
Cache-Control: max-age=0


HTTP/1.1 401 Unauthorized
Content-Length: 81
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/8.0
WWW-Authenticate: Basic realm="Service"
X-Powered-By: ASP.NET
Set-Cookie:    ARRAffinity=50b9234b61ec5f663e817ec57c430ca7b921bbcd842719dfc2bdc27374adea87;Path=/;Domain=********.azure-mobile.net
Date: Wed, 04 Jun 2014 21:38:56 GMT

<Error><Message>Authorization has been denied for this request.</Message></Error>

There is a workaround for enabling CORS in Mobile Services here: 这里有一个在移动服务中启用CORS的解决方法:

https://gist.github.com/HenrikFrystykNielsen/6c934be6c6c8fa9e4bc8 https://gist.github.com/HenrikFrystykNielsen/6c934be6c6c8fa9e4bc8

You don't need the Application_BeginRequest part -- requests/responses don't go through that code path -- they go through the OWIN pipeline. 您不需要Application_BeginRequest部分-请求/响应不通过该代码路径-它们通过OWIN管道。 Good thing is that you only need the gist above to get going. 好消息是,您只需要上面的要点即可开始。

Hope this helps! 希望这可以帮助!

Henrik 亨里克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM