在Azure的移动服务启用CORS - 选项未授权 [英] Enable CORS in Azure Mobile Services - OPTIONS not authorized

查看:259
本文介绍了在Azure的移动服务启用CORS - 选项未授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Azure移动服务创建的.NET Web服务。服务本身做工精细,但我想启用CORS。

我的Global.asax包含:

 保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
    HttpContext.Current.Response.AddHeader(访问控制允许原产地,*);
    如果(HttpContext.Current.Request.HttpMethod =OPTIONS!)回报;
    HttpContext.Current.Response.AddHeader(访问控制允许的方法,GET,POST);
    HttpContext.Current.Response.AddHeader(访问控制允许报头
        授权,来源,内容类型,接受,X-要求,随着X-谟的应用程序,X谟安装-ID);
    HttpContext.Current.Response.AddHeader(访问控制-max-age的,1728000);
    HttpContext.Current.Response.End();
}

我WebAPIConfig.cs包括:

 公共静态无效的注册()
    {
        ConfigOptions选项=新ConfigOptions();
        HttpConfiguration配置= ServiceConfig.Initialize(新ConfigBuilder(选项));        变种CORS =新EnableCorsAttribute(*,*,*,*);
        config.EnableCors(CORS);        config.Routes.MapHttpRoute(DefaultApiWithAction,原料药/ {控制器} / {行动});
    }

我的请求/响应:

 选项http://********.azure-mobile.net/API/MyLogin?username=username&password=password&email=testtest%40example.com&_=140191793307 HTTP / 1.1
主持人:******** azure-mobile.net。
用户代理:Mozilla的/ 5.0(Windows NT的6.2; WOW64; RV:29.0)的Gecko / 20100101火狐/ 29.0
接受:text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9 * / *; Q = 0.8
接受语言:EN-US,EN; Q = 0.5
接受编码:gzip,紧缩
来源:空
访问控制请求-方法:GET
访问控制请求报头:内容类型,X谟的应用程序,X谟安装-ID
连接:保持活动
缓存控制:最大年龄= 0
HTTP / 1.1 401未经授权
内容长度:81
内容类型:应用程序/ xml的;字符集= UTF-8
服务器:Microsoft-IIS / 8.0
WWW身份验证:基本境界=安培; QUOT;服务与; QUOT;
的X已启动方式:ASP.NET
设置Cookie: ARRAffinity=50b9234b61ec5f663e817ec57c430ca7b921bbcd842719dfc2bdc27374adea87;Path=/;Domain=********.azure-mobile.net
日期:星期三,2014年6月4日21时38分56秒GMT&放大器; LT;错误和放大器; GT;&放大器; LT;消息和; GT;授权已被拒绝了这个请求和放大器; LT; /消息和; GT;&放大器; LT; /错误和放大器; GT;


解决方案

有是在这里实现在移动服务CORS一种解决方法:

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

您不需要的Application_BeginRequest部分 - 请求/响应不去通过code路径 - 他们经过OWIN管道。好消息是,你只需要上面的要点走了。

希望这有助于!

亨里克

I am creating .Net web services using Azure Mobile Services. The services themselves work fine, but I want to enable CORS.

My Global.asax contains:

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:

    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:

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. Good thing is that you only need the gist above to get going.

Hope this helps!

Henrik

这篇关于在Azure的移动服务启用CORS - 选项未授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆