public async Task DefaultOptions(String pathInfo) { try { var rm = await RequestModel.CreateFromApiUrl(_baseController.Host, "_api/" + pathInfo); var ac = rm.CurrentCommand; if (!ValidAllowAddress(ac)) { return; } Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); Response.AddHeader("Access-Control-Allow-Credentials", "true"); Response.AddHeader("Access-Control-Max-Age", "60"); Response.AddHeader("Access-Control-Allow-Origin", ac.AllowOriginForCheck); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } _logger.LogApiError(ex.Message, Request.UserHostAddress, Guid.NewGuid()); } }
public async Task DefaultGET(String pathInfo) { Guid apiGuid = Guid.NewGuid(); try { _logger.LogApi($"get: {pathInfo}", Request.UserHostAddress, apiGuid); var rm = await RequestModel.CreateFromApiUrl(_baseController.Host, "_api/" + pathInfo); var ac = rm.CurrentCommand; if (ac.AllowOriginForCheck == null) { throw new RequestModelException($"'allowOrigin' is required for '{ac.command}' command"); } if (!ac.IsGet()) { throw new RequestModelException($"Method 'get' is required for '{ac.command}' command"); } if (!ValidAllowAddress(ac)) { return; } Response.AddHeader("Access-Control-Allow-Origin", ac.AllowOriginForCheck); switch (ac.type) { case CommandType.file: if (ac.file == null) { throw new RequestModelException($"'file' is required for '{ac.command}' command"); } GetFile(ac); break; case CommandType.clr: await ExecuteClrCommand(ac, GetDataToInvokeGet(ac.wrapper, apiGuid, ac.authorize), apiGuid); break; default: throw new NotImplementedException(nameof(DefaultGET)); } } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } Response.ContentType = "text/plain"; Response.Output.Write(ex.Message); } }
public async Task DefaultPOST(String pathInfo) { Guid apiGuid = Guid.NewGuid(); try { _logger.LogApi($"post: {pathInfo}", Request.UserHostAddress, apiGuid); var rm = await RequestModel.CreateFromApiUrl(_baseController.Host, "_api/" + pathInfo); var ac = rm.CurrentCommand; if (!ValidAllowAddress(ac)) { return; } Response.ContentType = "application/json"; Response.AddHeader("Access-Control-Allow-Origin", ac.AllowOriginForCheck); String json = null; Request.InputStream.Seek(0, SeekOrigin.Begin); // ensure using (var tr = new StreamReader(Request.InputStream)) { json = tr.ReadToEnd(); _logger.LogApi($"request: {json}", Request.UserHostAddress, apiGuid); } ExpandoObject dataToInvoke = JsonConvert.DeserializeObject <ExpandoObject>(json, new ExpandoObjectConverter()); if (dataToInvoke == null) { dataToInvoke = new ExpandoObject(); } if (!String.IsNullOrEmpty(ac.wrapper)) { ExpandoObject wrap = new ExpandoObject(); wrap.Set(ac.wrapper, dataToInvoke); dataToInvoke = wrap; } SetIdentityParams(dataToInvoke); await ExecuteCommand(ac, dataToInvoke, apiGuid); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } _logger.LogApiError(ex.Message, Request.UserHostAddress, apiGuid); _baseController.WriteExceptionStatus(ex, Response); } }
public async Task DefaultGET(String pathInfo) { try { _logger.LogApi($"get: {pathInfo}"); var rm = await RequestModel.CreateFromApiUrl(_baseController.Host, "_api/" + pathInfo); var ac = rm.CurrentCommand; if (ac.allowOrigin == null) { throw new RequestModelException($"'allowOrigin' is required for '{ac.command}' command"); } Response.AddHeader("Access-Control-Allow-Origin", ac.allowOrigin); switch (ac.type) { case CommandType.file: if (ac.file == null) { throw new RequestModelException($"'file' is required for '{ac.command}' command"); } GetFile(ac); break; case CommandType.clr: await ExecuteClrCommand(ac, GetDataToInvokeGet(ac.wrapper)); break; default: throw new NotImplementedException(); } } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } Response.ContentType = "text/plain"; Response.Output.Write(ex.Message); } }
public async Task DefaultPOST(String pathInfo) { Guid apiGuid = Guid.NewGuid(); Int32 errorCode = 0; try { _logger.LogApi($"post: {pathInfo}", Request.UserHostAddress, apiGuid); var rm = await RequestModel.CreateFromApiUrl(_baseController.Host, "_api/" + pathInfo); var ac = rm.CurrentCommand; if (!ValidAllowAddress(ac)) { return; } if (!ValidHosts(ac)) { return; } if (!ac.IsPost()) { throw new RequestModelException($"Method 'post' is required for '{ac.command}' command"); } errorCode = ac.errorCode; if (ac.authorize && !User.Identity.IsAuthenticated) { _logger.LogApiError("Unauthorized", Request.UserHostAddress, apiGuid); Response.StatusCode = 401; return; } Response.ContentType = MimeTypes.Application.Json; Response.AddHeader("Access-Control-Allow-Origin", ac.AllowOriginForCheck); String json = null; Request.InputStream.Seek(0, SeekOrigin.Begin); // ensure using (var tr = new StreamReader(Request.InputStream)) { json = tr.ReadToEnd(); _logger.LogApi($"request: {json}", Request.UserHostAddress, apiGuid); } ExpandoObject dataToInvoke = JsonConvert.DeserializeObject <ExpandoObject>(json, new ExpandoObjectConverter()); if (dataToInvoke == null) { dataToInvoke = new ExpandoObject(); } if (!String.IsNullOrEmpty(ac.wrapper)) { ExpandoObject wrap = new ExpandoObject(); wrap.Set(ac.wrapper, dataToInvoke); dataToInvoke = wrap; } SetIdentityParams(dataToInvoke, ac.authorize); await ExecuteCommand(ac, dataToInvoke, apiGuid); await _baseController.ProcessDbEvents(ac); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } _logger.LogApiError(ex.Message, Request.UserHostAddress, apiGuid); _baseController.WriteExceptionStatus(ex, Response, errorCode); } }