private void Page_Load(object sender, System.EventArgs e)
        {
            msgLabel.Text = "";



            if (IsPostBack)
            {
                Page.Validate();

                if (Page.IsValid)
                {
                    ClarifySession sess;

                    ClarifyLoginType loginType = userTypeContact.Checked ? ClarifyLoginType.Contact : ClarifyLoginType.User;

                    try
                    {
                        sess = fcApp.CreateSession(username.Text, password.Text, loginType);
                        Session["FCSessionID"] = sess.SessionID;

                        FormsAuthentication.RedirectFromLoginPage(username.Text, true);
                    }
                    catch (FCInvalidLoginException)
                    {
                        msgLabel.Text = "Invalid username or password.";
                    }
                }
            }
        }
        private IClarifySession getSession(string username, bool isConfigured = true, bool isObserved = true)
        {
            IClarifySession session;

            using (_logger.Push("Get session for {0}.".ToFormat(username)))
            {
                lock (SyncRoot)
                {
                    if (_agentSessionCacheByUsername.TryGetValue(username, out session))
                    {
                        if (_clarifyApplication.IsSessionValid(session.Id) && session.As <IClarifySessionProxy>().Session.SessionData != null)
                        {
                            _logger.LogDebug("Found valid session in cache.");
                            StateManager.ResetTimeout(session.Id);
                            return(session);
                        }

                        _logger.LogDebug("Ejecting invalid session.");
                        EjectSession(username, isObserved);
                    }

                    if (_agentSessionCacheByUsername.ContainsKey(username))
                    {
                        _logger.LogDebug("Found session (within the lock). Assuming it is valid because it must be very recent.");
                        return(_agentSessionCacheByUsername[username]);
                    }

                    //session = CreateSession(username, isConfigured, isObserved);
                    _logger.LogDebug("Creating missing session.");

                    var clarifySession = _clarifyApplication.CreateSession(username, ClarifyLoginType.User);
                    clarifySession.SetNullStringsToEmpty = true;

                    session = wrapSession(clarifySession);

                    _logger.LogInfo("Created session {0}.".ToFormat(clarifySession.SessionID));

                    _agentSessionCacheByUsername.Add(username, session);

                    _logger.LogDebug("{0} sessions are now in the cache.", _agentSessionCacheByUsername.Count);

                    visitSession(clarifySession, isConfigured, isObserved);
                }
            }

            return(session);
        }