private static void InternalOnPostAuthorizeRequest(object sender)
        {
            ExTraceGlobals.CoreCallTracer.TraceDebug(0L, "[OwaRequestHandler::InternalOnPostAuthorizeRequest] entry.");
            RequestDetailsLogger   getRequestDetailsLogger = OwaApplication.GetRequestDetailsLogger;
            SubActivityScopeLogger subActivityScopeLogger  = SubActivityScopeLogger.Create(getRequestDetailsLogger, OwaServerLogger.LoggerData.OnPostAuthorizeRequestLatencyDetails);
            HttpApplication        httpApplication         = (HttpApplication)sender;
            HttpContext            context = httpApplication.Context;

            if (!context.Request.IsAuthenticated && (context.Request.Url.LocalPath.EndsWith("service.svc", StringComparison.OrdinalIgnoreCase) || context.Request.Url.LocalPath.EndsWith("Speech.reco", StringComparison.OrdinalIgnoreCase)))
            {
                ExTraceGlobals.CoreCallTracer.TraceDebug <string>(0L, "[OwaRequestHandler::InternalOnPostAuthorizeRequest] unauthorized request. Request URL={0}.", context.Request.Url.OriginalString);
                context.Response.StatusCode = 401;
                httpApplication.CompleteRequest();
                return;
            }
            RequestContext requestContext = RequestContext.Get(httpApplication.Context);

            subActivityScopeLogger.LogNext("a");
            RequestDispatcher.DispatchRequest(requestContext);
            subActivityScopeLogger.LogNext("b");
            OwaRequestType requestType = requestContext.RequestType;

            RequestDispatcherUtilities.SetXFrameOptionsHeader(RequestContext.Current, requestType);
            subActivityScopeLogger.LogNext("c");
            if (context.User != null && context.User.Identity != null)
            {
                if (context.User.Identity is ClientSecurityContextIdentity)
                {
                    IMailboxContext mailboxContext = UserContextManager.GetMailboxContext(context, null, false);
                    subActivityScopeLogger.LogNext("d");
                    if (OwaRequestHandler.IsProxyLogonRequest(requestType))
                    {
                        ExTraceGlobals.CoreCallTracer.TraceDebug <OwaRequestType>(0L, "[OwaRequestHandler::InternalOnPostAuthorizeRequest] proxy logon request. RequestType={0}", requestType);
                        return;
                    }
                    RequestDetailsLogger.LogEvent(getRequestDetailsLogger, OwaServerLogger.LoggerData.CanaryValidationBegin);
                    bool   flag  = OwaRequestHandler.IsRequestWithCanary(context.Request, requestType, context.Request.IsAuthenticated);
                    bool   flag2 = OwaRequestHandler.IsAfterLogonRequest(context.Request);
                    string originalIdentitySid = OwaRequestHandler.GetOriginalIdentitySid(context);
                    CanaryLogEvent.CanaryStatus canaryStatus = CanaryLogEvent.CanaryStatus.None;
                    bool flag3 = !flag || flag2;
                    if (!flag3)
                    {
                        Canary15Cookie.CanaryValidationResult canaryValidationResult;
                        flag3         = Canary15Cookie.ValidateCanaryInHeaders(context, originalIdentitySid, Canary15Profile.Owa, out canaryValidationResult);
                        canaryStatus |= (CanaryLogEvent.CanaryStatus)canaryValidationResult;
                    }
                    OwaRequestHandler.UpdateCanaryStatus(ref canaryStatus, flag, CanaryLogEvent.CanaryStatus.IsCanaryNeeded);
                    OwaRequestHandler.UpdateCanaryStatus(ref canaryStatus, flag3, CanaryLogEvent.CanaryStatus.IsCanaryValid);
                    OwaRequestHandler.UpdateCanaryStatus(ref canaryStatus, flag2, CanaryLogEvent.CanaryStatus.IsCanaryAfterLogonRequest);
                    Canary15Cookie canary15Cookie = Canary15Cookie.TryCreateFromHttpContext(context, originalIdentitySid, Canary15Profile.Owa);
                    OwaRequestHandler.UpdateCanaryStatus(ref canaryStatus, canary15Cookie.IsAboutToExpire, CanaryLogEvent.CanaryStatus.IsCanaryAboutToExpire);
                    OwaRequestHandler.UpdateCanaryStatus(ref canaryStatus, canary15Cookie.IsRenewed, CanaryLogEvent.CanaryStatus.IsCanaryRenewed);
                    subActivityScopeLogger.LogNext("e");
                    bool flag4 = flag || canary15Cookie.IsAboutToExpire;
                    if (flag4)
                    {
                        canary15Cookie = new Canary15Cookie(originalIdentitySid, Canary15Profile.Owa);
                    }
                    if (canary15Cookie.IsRenewed || flag4)
                    {
                        context.Response.SetCookie(canary15Cookie.HttpCookie);
                        CanaryLogEvent logEvent = new CanaryLogEvent(context, mailboxContext, canaryStatus, canary15Cookie.CreationTime, canary15Cookie.LogData);
                        OwaServerLogger.AppendToLog(logEvent);
                        subActivityScopeLogger.LogNext("f");
                    }
                    if (flag3)
                    {
                        RequestDetailsLoggerBase <RequestDetailsLogger> .SafeSetLogger(getRequestDetailsLogger, OwaServerLogger.LoggerData.CanaryCreationTime, canary15Cookie.CreationTime);

                        RequestDetailsLoggerBase <RequestDetailsLogger> .SafeSetLogger(getRequestDetailsLogger, OwaServerLogger.LoggerData.CanaryLogData, canary15Cookie.LogData);

                        subActivityScopeLogger.LogNext("g");
                    }
                    else
                    {
                        if (RequestDispatcherUtilities.IsDownLevelClient(context, false))
                        {
                            throw new OwaCanaryException(Canary15Profile.Owa.Name, canary15Cookie.Value);
                        }
                        context.Response.StatusCode = 449;
                        context.Response.End();
                    }
                    RequestDetailsLoggerBase <RequestDetailsLogger> requestDetailsLogger = getRequestDetailsLogger;
                    Enum key = OwaServerLogger.LoggerData.CanaryStatus;
                    int  num = (int)canaryStatus;
                    RequestDetailsLoggerBase <RequestDetailsLogger> .SafeSetLogger(requestDetailsLogger, key, num.ToString("X"));

                    RequestDetailsLogger.LogEvent(getRequestDetailsLogger, OwaServerLogger.LoggerData.CanaryValidationEnd);
                    subActivityScopeLogger.LogEnd();
                    return;
                }
            }
            else
            {
                ExTraceGlobals.CoreCallTracer.TraceError(0L, "[OwaRequestHandler::InternalOnPostAuthorizeRequest] httpContext.User or httpContext.User.Identity is <NULL>.");
            }
        }