簡體   English   中英

在Azure移動服務中啟用CORS-未授權的選項

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

我正在使用Azure移動服務創建.Net Web服務。 服務本身可以正常工作,但是我想啟用CORS。

我的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();
}

我的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}");
    }

我的要求/回應:

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>

這里有一個在移動服務中啟用CORS的解決方法:

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

您不需要Application_BeginRequest部分-請求/響應不通過該代碼路徑-它們通過OWIN管道。 好消息是,您只需要上面的要點即可開始。

希望這可以幫助!

亨里克

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM