ClearCanvas.Web.Enterprise.Authentication.LoginService.DoRenewSession C# (CSharp) Method

DoRenewSession() private method

private DoRenewSession ( string tokenId, bool validateOnly ) : ClearCanvas.Web.Enterprise.Authentication.SessionInfo
tokenId string
validateOnly bool
return ClearCanvas.Web.Enterprise.Authentication.SessionInfo
		private SessionInfo DoRenewSession(string tokenId, bool validateOnly)
		{
			SessionInfo sessionInfo = SessionCache.Instance.Find(tokenId);
			if (sessionInfo == null)
			{
				throw new Exception(String.Format("Unexpected error: session {0} does not exist in the cache", tokenId));
			}

			var request = new ValidateSessionRequest(sessionInfo.Credentials.UserName,
													 sessionInfo.Credentials.SessionToken)
			              	{
			              		GetAuthorizations = true,
			              		ValidateOnly = validateOnly
			              	};

			SessionToken newToken = null;
			Platform.GetService(
				delegate(IAuthenticationService service)
					{

						ValidateSessionResponse response = service.ValidateSession(request);

						//If we're renewing, might as well renew well before expiry. If we're only validating,
						//we don't want to force a call to the server unless it's actually expired.
						var addSeconds = 0.0;

						//Minor hack - the ImageServer client shows a little bar at the top of the page counting down to session timeout,
						//and allowing the user to cancel. This timeout value determines when that bar shows up. If, however, the 
						//validate response is being cached, we need to make sure we hit the server - otherwise the bar doesn't go away.
						double.TryParse(ConfigurationManager.AppSettings.Get("ClientTimeoutWarningMinDuration"), out addSeconds);
						addSeconds = Math.Max(addSeconds, 30);

						if (Platform.Time.AddSeconds(addSeconds) >= response.SessionToken.ExpiryTime)
						{
							Platform.Log(LogLevel.Debug, "Session is at or near expiry; bypassing cache.");

							//The response likely came from the cache, and we want to make sure we get a real validation response.
							//Note that this only really matters when 2 processes are sharing a session, like ImageServer and Webstation.
							using (new ResponseCacheBypassScope())
							{
								response = service.ValidateSession(request);
							}
						}

						// update session info
						string id = response.SessionToken.Id;
						newToken = SessionCache.Instance.Renew(id, response.SessionToken.ExpiryTime);
						sessionInfo.Credentials.Authorities = response.AuthorityTokens;
						sessionInfo.Credentials.SessionToken = newToken;

						if (Platform.IsLogLevelEnabled(LogLevel.Debug))
						{
							Platform.Log(LogLevel.Debug, "Session {0} for {1} is renewed. Valid until {2}", id,
							             sessionInfo.Credentials.UserName, newToken.ExpiryTime);
						}
					});

			return sessionInfo;
		}