using System; using System.Collections.Generic; using System.Configuration; using System.Web; using GTech.Solution.Api.Common.Common; namespace GTech.Solution.Api { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { var context = HttpContext.Current; var request = context.Request; var response = context.Response; HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "*"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "*"); HttpContext.Current.Response.AddHeader("Access-Control-Request-Headers", "*"); HttpContext.Current.Response.End(); } CheckAccessPermission(request, response); } private void CheckAccessPermission(HttpRequest request, HttpResponse response) { try { var requestHeader = request.Headers; var requestUrl = request.Url; var token = request.Headers["Authorization"]; var isNeedValidateJwtToken = true; if (requestUrl.AbsolutePath == "/") { isNeedValidateJwtToken = false; } else { var IgnoringAntPathRequestMatcher = new List(); var ignoringAntPathRequestMatcherStr = ConfigurationManager.AppSettings["IgnoringAntPathRequestMatcher"]; if (!string.IsNullOrEmpty(ignoringAntPathRequestMatcherStr)) { var ignoringAntPathRequestMatcherArray = ignoringAntPathRequestMatcherStr.Split(','); IgnoringAntPathRequestMatcher.AddRange(ignoringAntPathRequestMatcherArray); } IgnoringAntPathRequestMatcher.ForEach(en => { if (requestUrl.AbsolutePath.Contains(en)) { isNeedValidateJwtToken = false; } }); } if (isNeedValidateJwtToken) { TokenGenerator.ValidateJwtToken(token); var userbasic = TokenGenerator.GetUserbasic(token); if (userbasic != null) { //Check permissions } } } catch (Exception ex) { response.ContentType = "application/json"; response.Write(ex.Message); response.End(); } } protected void Application_EndRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); try { } catch { } } protected void Application_End(object sender, EventArgs e) { try { } catch { } } private ResponseCompressionType GetCompressionMode(HttpRequest request) { string acceptEncoding = request.Headers["Accept-Encoding"]; if (string.IsNullOrEmpty(acceptEncoding)) { return ResponseCompressionType.None; } acceptEncoding = acceptEncoding.ToUpperInvariant(); if (acceptEncoding.Contains("GZIP")) { return ResponseCompressionType.GZip; } else if (acceptEncoding.Contains("DEFLATE")) { return ResponseCompressionType.Deflate; } else { return ResponseCompressionType.None; } } private enum ResponseCompressionType { None, GZip, Deflate } } }