Exemple #1
0
        private static void InsertIntoCache(HttpContext httpContext, IMailboxContext userContext)
        {
            UserContextManager.UserContextCacheWrapper userContextCacheWrapper = (UserContextManager.UserContextCacheWrapper)HttpRuntime.Cache.Get(userContext.Key.ToString());
            if (userContextCacheWrapper == null)
            {
                userContextCacheWrapper = new UserContextManager.UserContextCacheWrapper
                {
                    Lock = new OwaRWLockWrapper()
                };
            }
            UserContextManager.TombstoneExistingUserContext(userContextCacheWrapper);
            userContextCacheWrapper.UserContext = userContext;
            int num = UserContextManager.UserContextTimeout;

            if (UserAgentUtilities.IsMonitoringRequest(httpContext.Request.UserAgent))
            {
                num = 1;
                if (userContext.UserPrincipalName.EndsWith(".exchangemon.net", StringComparison.InvariantCultureIgnoreCase))
                {
                    ExMonitoringRequestTracker.Instance.ReportMonitoringRequest(httpContext.Request);
                }
            }
            HttpRuntime.Cache.Insert(userContext.Key.ToString(), userContextCacheWrapper, null, DateTime.MaxValue, new TimeSpan((long)num * 600000000L), CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(UserContextManager.UserContextRemovedCallback));
            userContext.LogBreadcrumb("Cached:Key=" + userContext.Key.ToString());
        }
Exemple #2
0
        internal static MailboxSession GetClonedMailboxSession(Guid mailboxGuid, CultureInfo cultureInfo, LogonType logonType, string userContextKey, ExchangePrincipal exchangePrincipal, IADOrgPerson person, ClientSecurityContext clientSecurityContext, GenericIdentity genericIdentity, bool unifiedLogon)
        {
            MailboxSession clonedMailboxSession = null;
            UserContext    userContext          = null;
            string         expectedMailboxKey   = string.Empty;

            if (!string.IsNullOrEmpty(userContextKey))
            {
                try
                {
                    OwaDiagnostics.SendWatsonReportsForGrayExceptions(delegate()
                    {
                        UserContextManager.UserContextCacheWrapper userContextCacheWrapper = (UserContextManager.UserContextCacheWrapper)HttpRuntime.Cache.Get(userContextKey);
                        if (userContextCacheWrapper != null && userContextCacheWrapper.UserContext != null && userContextCacheWrapper.UserContext.TerminationStatus == UserContextTerminationStatus.NotTerminate)
                        {
                            userContext = (userContextCacheWrapper.UserContext as UserContext);
                            if (userContext != null && userContext.FeaturesManager != null && userContext.FeaturesManager.ServerSettings.OwaMailboxSessionCloning.Enabled && userContext.UserCulture != null && !userContext.IsExplicitLogon)
                            {
                                expectedMailboxKey   = AccessingPrincipalTiedCache.BuildKeyCacheKey(mailboxGuid, cultureInfo, logonType);
                                clonedMailboxSession = userContext.CloneMailboxSession(expectedMailboxKey, exchangePrincipal, person, clientSecurityContext, genericIdentity, unifiedLogon);
                            }
                        }
                    });
                }
                catch (GrayException ex)
                {
                    ExTraceGlobals.UserContextCallTracer.TraceError(0L, string.Format("Exception while cloning MailboxSession, MailboxKey: {0}, Exception: {1}", expectedMailboxKey, ex.Message));
                }
            }
            return(clonedMailboxSession);
        }
Exemple #3
0
 private static void UserContextRemovedCallback(string key, object value, CacheItemRemovedReason reason)
 {
     UserContextManager.UserContextCacheWrapper userContextCacheWrapper = value as UserContextManager.UserContextCacheWrapper;
     if (userContextCacheWrapper != null && userContextCacheWrapper.UserContext != null)
     {
         userContextCacheWrapper.UserContext.LogBreadcrumb("UserContextRemovedCallback.  Key=" + userContextCacheWrapper.UserContext.Key.ToString());
     }
     ThreadPool.QueueUserWorkItem(new WaitCallback(UserContextManager.UserContextRemoved), new object[]
     {
         key,
         value,
         reason
     });
 }
Exemple #4
0
        private static void TombstoneExistingUserContext(UserContextManager.UserContextCacheWrapper wrapper)
        {
            IMailboxContext userContext = wrapper.UserContext;

            if (userContext != null)
            {
                userContext.LogBreadcrumb("This context will be tombstoned.");
                string key = string.Format("Tombstoned-{0}", userContext.Key.ToString());
                UserContextManager.UserContextCacheWrapper value = new UserContextManager.UserContextCacheWrapper
                {
                    Lock        = new OwaRWLockWrapper(),
                    UserContext = userContext
                };
                HttpRuntime.Cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1.0), CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(UserContextManager.UserContextRemovedCallback));
            }
        }
Exemple #5
0
 private static OwaRWLockWrapper GetUserContextKeyLock(string userContextKeyString)
 {
     UserContextManager.UserContextCacheWrapper userContextCacheWrapper = (UserContextManager.UserContextCacheWrapper)HttpRuntime.Cache.Get(userContextKeyString);
     if (userContextCacheWrapper == null)
     {
         lock (UserContextManager.lockObject)
         {
             userContextCacheWrapper = (UserContextManager.UserContextCacheWrapper)HttpRuntime.Cache.Get(userContextKeyString);
             if (userContextCacheWrapper == null)
             {
                 userContextCacheWrapper = new UserContextManager.UserContextCacheWrapper
                 {
                     Lock = new OwaRWLockWrapper()
                 };
                 HttpRuntime.Cache.Insert(userContextKeyString, userContextCacheWrapper, null, DateTime.MaxValue, TimeSpan.FromMinutes(2.5), CacheItemPriority.NotRemovable, null);
             }
         }
     }
     return(userContextCacheWrapper.Lock);
 }
Exemple #6
0
        private static IMailboxContext GetMailboxContextFromCache(UserContextKey userContextKey, bool lockUserContextKey)
        {
            ExTraceGlobals.UserContextCallTracer.TraceDebug(0L, "UserContextManager.TryGetUserContextFromCache");
            if (userContextKey == null)
            {
                ExTraceGlobals.UserContextCallTracer.TraceDebug(0L, "UserContextManager.TryGetUserContextFromCache: No UserContextKey provided, returning null.");
                return(null);
            }
            UserContextManager.UserContextCacheWrapper userContextCacheWrapper = null;
            IMailboxContext mailboxContext = null;
            bool            flag           = false;

            try
            {
                ExTraceGlobals.UserContextCallTracer.TraceDebug <UserContextKey>(0L, "Attempting to fetch user context from the cache.  Key={0}", userContextKey);
                userContextCacheWrapper = (UserContextManager.UserContextCacheWrapper)HttpRuntime.Cache.Get(userContextKey.ToString());
                if (userContextCacheWrapper != null && userContextCacheWrapper.UserContext != null)
                {
                    if (lockUserContextKey)
                    {
                        if (userContextCacheWrapper.Lock == null)
                        {
                            throw new OwaLockException("UserContextManager::GetUserContextFromCache cannot get a lock from UserContextCacheWrapper");
                        }
                        if (!userContextCacheWrapper.Lock.IsWriterLockHeld)
                        {
                            if (!userContextCacheWrapper.Lock.LockWriterElastic(3000))
                            {
                                throw new OwaLockTimeoutException("UserContextManager::GetUserContextFromCache cannot acquire a write lock in a time fashion");
                            }
                            flag           = true;
                            mailboxContext = userContextCacheWrapper.UserContext;
                            ExTraceGlobals.UserContextCallTracer.TraceDebug <UserContextKey, IMailboxContext>(0L, "User context found in cache. Key={0}, User context instance={1}", userContextKey, mailboxContext);
                        }
                    }
                    else
                    {
                        mailboxContext = userContextCacheWrapper.UserContext;
                        ExTraceGlobals.UserContextCallTracer.TraceDebug <UserContextKey, IMailboxContext>(0L, "User context found in cache. Key={0}, User context instance={1}", userContextKey, mailboxContext);
                    }
                }
                else
                {
                    ExTraceGlobals.UserContextCallTracer.TraceDebug <string>(0L, "An object for this user context ID value is not present in the cache (probably was expired), so we are going to reuse the user context ID value for the new session", userContextKey.UserContextId);
                }
            }
            finally
            {
                if (mailboxContext != null && mailboxContext.TerminationStatus == UserContextTerminationStatus.TerminatePending)
                {
                    UserContextManager.TerminateSession(mailboxContext, mailboxContext.AbandonedReason);
                    mailboxContext = null;
                }
                if (flag && userContextCacheWrapper != null && userContextCacheWrapper.Lock != null)
                {
                    userContextCacheWrapper.Lock.ReleaseWriterLock();
                }
                if (mailboxContext != null && mailboxContext.State != UserContextState.Active)
                {
                    mailboxContext = null;
                }
            }
            return(mailboxContext);
        }
Exemple #7
0
        private static void UserContextRemoved(object parameters)
        {
            bool flag = false;

            try
            {
                if (flag)
                {
                }
            }
            finally
            {
                try
                {
                    object[] array = parameters as object[];
                    string   text  = array[0] as string;
                    object   obj   = array[1];
                    CacheItemRemovedReason cacheItemRemovedReason = (CacheItemRemovedReason)array[2];
                    UserContextManager.UserContextCacheWrapper userContextCacheWrapper = obj as UserContextManager.UserContextCacheWrapper;
                    if (userContextCacheWrapper != null)
                    {
                        if (userContextCacheWrapper.UserContext != null)
                        {
                            userContextCacheWrapper.UserContext.LogBreadcrumb("UserContextRemoved Invoked.  Key=" + text);
                        }
                        if (userContextCacheWrapper.Lock != null && !userContextCacheWrapper.Lock.IsWriterLockHeld)
                        {
                            try
                            {
                                if (userContextCacheWrapper.Lock.LockWriterElastic(3000) && userContextCacheWrapper.UserContext != null)
                                {
                                    ExTraceGlobals.UserContextTracer.TraceDebug <string, IMailboxContext, int>(0L, "Removing user context from cache, Key={0}, User context instance={1}, Reason={2}", text, userContextCacheWrapper.UserContext, (int)cacheItemRemovedReason);
                                    userContextCacheWrapper.UserContext.LogBreadcrumb("UserContextRemoved Terminating.  Key=" + userContextCacheWrapper.UserContext.Key.ToString());
                                    UserContextManager.TerminateSession(userContextCacheWrapper.UserContext, cacheItemRemovedReason);
                                    userContextCacheWrapper.UserContext.LogBreadcrumb("UserContextRemoved Terminated.  Key=" + userContextCacheWrapper.UserContext.Key.ToString());
                                    userContextCacheWrapper.UserContext.TerminationStatus = UserContextTerminationStatus.TerminateCompleted;
                                }
                            }
                            finally
                            {
                                if (userContextCacheWrapper.Lock.IsWriterLockHeld)
                                {
                                    userContextCacheWrapper.Lock.ReleaseWriterLock();
                                }
                            }
                        }
                        if (userContextCacheWrapper.UserContext != null && userContextCacheWrapper.UserContext.TerminationStatus != UserContextTerminationStatus.TerminateCompleted)
                        {
                            userContextCacheWrapper.UserContext.TerminationStatus = UserContextTerminationStatus.TerminatePending;
                            userContextCacheWrapper.UserContext.AbandonedReason   = cacheItemRemovedReason;
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                }
                catch (Exception ex)
                {
                    OwaDiagnostics.LogEvent(ClientsEventLogConstants.Tuple_UserContextTerminationError, string.Empty, new object[]
                    {
                        ex.ToString()
                    });
                    ExTraceGlobals.UserContextTracer.TraceDebug(0L, "Sending watson report");
                    ExWatson.SendReport(ex, ReportOptions.None, null);
                }
            }
        }