Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXTREME 13.1 Demos/DXSK8/DXSK8.Service/
Upload File :
Current File : C:/Users/Public/Documents/DXTREME 13.1 Demos/DXSK8/DXSK8.Service/CorsSupport.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DataServicesCORS {

    // NOTE
    // The following change to web.config is required
    // <system.serviceModel>
    //    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 

    static class CorsSupport {

        public static void HandlePreflightRequest(HttpContext context) {
            var req = context.Request;
            var res = context.Response;
            var origin = req.Headers["Origin"];

            if(!String.IsNullOrEmpty(origin)) {
                res.AddHeader("Access-Control-Allow-Origin", origin);
                res.AddHeader("Access-Control-Allow-Credentials", "true");

                var methods = req.Headers["Access-Control-Request-Method"];
                var headers = req.Headers["Access-Control-Request-Headers"];

                if(!String.IsNullOrEmpty(methods))
                    res.AddHeader("Access-Control-Allow-Methods", methods);

                if(!String.IsNullOrEmpty(headers))
                    res.AddHeader("Access-Control-Allow-Headers", headers);

                if(req.HttpMethod == "OPTIONS") {
                    res.StatusCode = 204;
                    res.End();
                }
            }
        }
        
    }
}