Aug 30, 2016

CORS with IIS

I've struggled quite a bit to get CORS working with an .NET web application hosted by IIS, but finally got it working with Windows authentication.

Here are the necessary requirements:

  1. The application pool must be running in Integrated mode, not Classic mode.
  2. You have to add a simple  HTTP module to your web service C# code to respond to the OPTIONS call without authentication: 
  3. public class CORSModule : IHttpModule
    {
        public void Dispose() { }
     
        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += delegate
            {
                if (context.Request.HttpMethod == "OPTIONS")
                {
                    var response = context.Response;
                    response.StatusCode = (int)HttpStatusCode.OK;
                }
            };
        }
    }
     
  4. You need to add these Header lines to the web.config:
  5. <?xml version="1.0" encoding="utf-8"?>
    <configuration>
     <system.webServer>
      <httpProtocol>
         <customHeaders>
            <add name="Access-Control-Allow-Origin" value="https://myserver" />
            <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
            <add name="Access-Control-Allow-Headers" value="Accept,Authorization,Content-Type,SOAPAction,X-RequestDigest" />
     <add name="Access-Control-Request-Headers" value="Content-Type,Authorization,Accept,SOAPAction,X-Requested-With" />
            <add name="Access-Control-Allow-Credentials" value="true" />
          </customHeaders>
        </httpProtocol>
      <modules>
        <add name="CORSModule" type="CORSModule" />
      </modules>
      </system.webServer>
    </configuration>
    </configuration>
     
  6. You need to add the "WithCredentials: true" to your JavaScript Ajax call to pass the Windows credentials if you're using Windows authentication.

Jun 17, 2016

SharePoint BCS: External content type not available for creating External List

I recently created a simple external content type for a WCF service in SharePoint 2013, but though I could create an external column that worked perfectly, the type was not available as an external list.

My problem was that I had added a PageNumber filter for the Finder (Read list items operation) to limit the items returned and activate pagination.

It seems External Lists do not support this and upon removing the filter from the operation, my content type was now available. This does work for SQL based external types.

Jan 25, 2016

Azure web app won't download my file because of the mimetype

If you have a non-standard file without a common mimetype (eg. dwg files for AutoCAD), Azure Web App sites won't serve them and instead give a 404 error.

This is a policy decision dating back to IIS 6.0.

The fix is simply adding the unknown mimetype to the web.config for the Azure Web App: https://www.iis.net/configreference/system.webserver/staticcontent/mimemap

For example:

<configuration>
   <system.webServer>
      <staticContent>
         <mimeMap fileExtension=".dwg" mimeType="application/acad" />
      </staticContent>
   </system.webServer>
</configuration>
 

Broken connection between Jetpack and Wordpress.com

I had a problem with another blog I maintain in a hosted WordPress installation that uses the Jetpack plugin for statistics and other services.  Upon upgrading some modules, the Jetpack plugin lost the connection to Wordpress.com and restoring the database didn't help.  I didn't know the username or email associated with the WordPress.com account and panic began to set in.

In desperation, I contacted Wordpress support and they informed me that I could simply create a new Wordpress.com account and if I connect it to the same domain, all the site information would be maintained, as all data is stored with the domain identity.

It worked like a charm!  Phew!!