public override void WebInitialize(WebCallContext webContext) { base.WebInitialize(webContext); if (string.IsNullOrWhiteSpace(Settings.RedirectUrl)) { // Initialize RedirectUrl; get service base address and set proper redirect URL // It is convenient to do it automatically here - so that it works automatically in real world apps, in any environment - test, staging or production. var uri = new Uri(webContext.RequestUrl); var baseAddress = uri.GetComponents(UriComponents.Scheme | UriComponents.HostAndPort, UriFormat.Unescaped); var baseAddressR = baseAddress.Replace("localhost", "127.0.0.1"); //By default we use IP address for local testing, this is required for most oauth servers Settings.RedirectUrl = baseAddressR + "/api/oauth_redirect"; } }
public override void HandleRequest(WebCallContext context, HttpRequestMessage request) { var versions = GetIncomingValue(context); if (string.IsNullOrWhiteSpace(versions)) return; //Parse var arrVersions = versions.Split(','); if (arrVersions.Length < 1) return; int value; if (int.TryParse(arrVersions[0], out value)) context.MinUserSessionVersion = value; if (arrVersions.Length > 1 && int.TryParse(arrVersions[1], out value)) context.MinCacheVersion = value; }
protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext) { //Note: when exception is thrown here, it is not routed to exc filter, everything just crashes, // so be careful not to throw anything in controller.Initialize base.Initialize(controllerContext); try { WebContext = WebHelper.GetWebCallContext(this); if(WebContext == null) return; OpContext = WebContext.OperationContext; ErrorLog = OpContext.App.GetService<IErrorLogService>(); } catch(Exception ex) { System.Diagnostics.Trace.WriteLine("Exception in controller.Initialize: " + ex.ToLogString()); if(ErrorLog != null) ErrorLog.LogError(ex, OpContext); } }
//Utilities protected string GetIncomingValue(WebCallContext context) { if (!Direction.IsSet(WebTokenDirection.Input)) //it is not input token return null; switch (this.TokenType) { case WebTokenType.Cookie: var cookies = context.GetIncomingCookies(this.TokenName); if (cookies == null) return null; if (cookies.Count == 1) return cookies[0].Value; return string.Join(";", cookies.Select(ck => ck.Value)); case WebTokenType.Header: var values = context.GetIncomingHeader(this.TokenName); if (values == null || values.Count == 0) return null; if (values.Count == 1) return values[0]; return string.Join(";", values); }//switch return null; }
public override void HandleResponse(WebCallContext context, HttpResponseMessage response) { var versions = context.MinUserSessionVersion + "," + context.MinCacheVersion; context.OutgoingHeaders.Add(this.TokenName, versions); }
public static void MarkConfidential(this WebCallContext context) { context.Flags |= WebCallFlags.Confidential; }
public virtual void WebInitialize(Web.WebCallContext webContext) { }
public WebCallInfo(EntityApp app, WebCallContextHandlerSettings settings, HttpRequestMessage request) { Request = request; WebContext = new WebCallContext(request, app.TimeService.UtcNow, app.TimeService.ElapsedMilliseconds, GetIncomingCookies, GetIncomingHeaderValues); WebContext.OperationContext = new OperationContext(app, UserInfo.Anonymous, WebContext, settings.ConnectionReuseMode); Request.Properties[WebCallContext.WebCallContextKey] = WebContext; WebContext.RequestUrl = request.RequestUri.ToString(); WebContext.HttpMethod = request.Method.ToString().ToUpperInvariant(); WebContext.RequestSize = request.Content.GetLength(); //Check if it is one of the sensitive URLs var path = request.RequestUri.LocalPath; // The only way to get IPaddress it seems is thru use of HttpContext (from ASP.NET host). // NOTE: it is available only under ASP.NET/IIS host, not in self-hosting scenario var ctxWrapper = WebHelper.GetHttpContextWrapper(request); if (ctxWrapper != null) { //IIS hosting WebContext.Referrer = ctxWrapper.Request.UrlReferrer + string.Empty; WebContext.IPAddress = ctxWrapper.Request.UserHostAddress; } else { //Self hosting // webCallContext.IPAddress = "(unknown)"; } //Set log level for this call WebContext.OperationContext.LogLevel = settings.LogLevel; }
private Guid? LogError(Exception exception, WebCallContext webCallInfo) { if(_errorLog == null) return null; var id = _errorLog.LogError(exception, webCallInfo.OperationContext); return id; }
public WebCallEventArgs(WebCallContext webContext) { WebContext = webContext; }
protected void SetOutgoingValue(WebCallContext context, string value) { if (!Direction.IsSet(WebTokenDirection.Output)) //it is not output token return; switch (this.TokenType) { case WebTokenType.Cookie: context.OutgoingCookies.Add(new System.Net.Cookie(this.TokenName, value)); return; case WebTokenType.Header: context.OutgoingHeaders.Add(this.TokenName, value); return; }//switch }
public virtual void HandleResponse(WebCallContext context, HttpResponseMessage response) { }
public virtual void HandleRequest(WebCallContext context, HttpRequestMessage request) { }
public static void SetHeaders(this HttpResponseMessage response, WebCallContext webContext) { foreach(var kv in webContext.OutgoingHeaders) { if (kv.Key.StartsWith("Content-", StringComparison.InvariantCultureIgnoreCase)) { if(response.Content == null) continue; var headers = response.Content.Headers; if (headers.Contains(kv.Key)) headers.Remove(kv.Key); headers.Add(kv.Key, kv.Value); } else { var headers = response.Headers; if (headers.Contains(kv.Key)) headers.Remove(kv.Key); headers.Add(kv.Key, kv.Value); } } }
public void Log(WebCallContext webContext) { _backgroundSaveService.AddObject(webContext); }