Asp.net MVC 4 CORS

To allow cross-domain in asp.net it’s enough to modify web.config file adding this configuration:

<system.webServer>

    ...
    ...
    ...
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Headers" value="accept, maxdataserviceversion, origin, x-requested-with, dataserviceversion, Content-Type" />
        <!--<add name="Access-Control-Allow-Origin" value="*" />-->
        <add name="Access-Control-Request-Headers" value="X-Custom-Header" />
        <add name="Access-Control-Max-Age" value="1728000" />
        <add name="Access-Control-Allow-Methods" value="OPTIONS, GET, POST, PUT, DELETE" />
        <add name="Access-Control-Allow-Credentials" value="true" />
       
      </customHeaders>
      
    </httpProtocol>
    <directoryBrowse enabled="true" />
    <security>
      <requestFiltering>
        <fileExtensions>
          <add fileExtension=".json" allowed="true" />
        </fileExtensions>
        <verbs>
          <add verb="GET" allowed="true" />
          <add verb="POST" allowed="true" />
          <add verb="OPTIONS" allowed="true" />
          <add verb="DELETE" allowed="true" />
          <add verb="PUT" allowed="true" />
          <add verb="REMOVE" allowed="true" />
        </verbs>
      </requestFiltering>
    </security>
    ...
    ...
    ...

If you are using Chrome you don’t need any other configurations. If you user Safari (and iphone app built with phonegap, sencha cmd ecc..) you need to add in global.asax:


void Application_PreSendRequestHeaders(Object sender, EventArgs e)
{
     Response.Cache.SetCacheability(HttpCacheability.NoCache);
}

And in web.xml add

<authentication mode="Forms">
  <forms cookieless="UseCookies"/>
</authentication
Annunci
Tagged with: , , , , , , , , ,
Pubblicato su Uncategorized

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: