// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using WS; namespace WS.WebApi { public class RepoServiceHttpHeaderController : ApiController { private static DataService ds = new DataService(); // Web API does not support singleton mode private void CheckAccess() { if(!Request.Headers.Contains("DataServicePassword") || Request.Headers.GetValues("DataServicePassword").First() != "Top secret") { throw new Exception("Not authorized"); } } [HttpGet] public IEnumerable GetAllData() { CheckAccess(); lock(ds) { return ds.GetAll(); } } [HttpGet] public Data GetDataById(int id) { CheckAccess(); lock(ds) { return ds.GetOne(id); } } [HttpPost] public void PostData(Data o) { CheckAccess(); lock(ds) { ds.Add(o); } } } }