protected void OnValidateCredentials(object source, ServerValidateEventArgs args)
 {
     try
     {
         var service     = new WebServiceAPI();
         var credentials = new WindowsCredentials()
         {
             WinUserName = txtUserName.Text,
             UserName    = txtUserName.Text,
             Password    = txtPassword.Text,
             WinDomain   = txtDomain.Text,
             Domain      = txtDomain.Text
         };
         args.IsValid = service.IsCredentialsValid(credentials);
         if (args.IsValid)
         {
             SecureStoreManager.SetExtentrixWindowsCredentials(LogLocation, SPContext.Current.Web.CurrentUser, credentials);
             SecureStoreManager.SetCredentialsLiveTime(CredentialLiveTime);
             Credentials = credentials;
         }
     }
     catch (Exception ex)
     {
         Logger.Default.Error(LogLocation, "User - " + Page.Request.LogonUserIdentity.Name + " ; Error = " + ex.Message, ex);
         AppMultiView.ActiveViewIndex = 5;
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            ExtentrixWIWebPart wp = getWebPartCurrentInstance();

            //set appearance properties
            MainBodyPanel.BorderWidth = MainBodyBorderWidth;

            MainBodyPanel.BorderColor = MainBodyBorderColor;
            MainBodyPanel.BackColor   = MainBodyBackColor;
            ToolBarPanel.BackColor    = ToolBarBackColor;

            linkIcaClientUrl.DataBind();
            fldIcaClientVersion.DataBind();

            if (!Page.IsPostBack)
            {
                fldShowExpirationMessage.DataBind();
            }
            else
            {
                fldShowExpirationMessage.Value = "";
            }

            //Page.Response.Cache.SetNoServerCaching();

            //to create current folder Application
            if (Page.Session[CurrentFolder] == null)
            {
                Page.Session[CurrentFolder] = string.Empty;
            }

            //fill view switcher
            if (!Page.IsPostBack)
            {
                FillViewSwitcher();
            }

            var queryString = Page.Request.QueryString;

            if (queryString.HasKeys() &&
                !string.IsNullOrEmpty(queryString["app"]) &&
                !string.IsNullOrEmpty(queryString["farmName"]))
            {
                // Lunch application passed by query string
                string id       = queryString["app"].ToString();
                string frmName  = queryString["farmName"].ToString();
                string hostName = wp.getCurrentHost();

                if (!IsUseSecureStore)
                {
                    wp.launchApplication(wp.GetCredentials(SPContext.Current.Web.CurrentUser), id, hostName, frmName);
                }
                else
                {
                    var credentials = Credentials ?? SecureStoreManager.GetExtentrixWindowsCredentials(Page, LogLocation, SPContext.Current.Web.CurrentUser);
                    if (credentials != null)
                    {
                        if (CredentialLiveTime != -1)
                        {
                            if (!SecureStoreManager.IsCredentialsExpired(CredentialLiveTime))
                            {
                                wp.launchApplication(credentials, id, hostName, frmName);
                            }
                            else
                            {
                                SecureStoreManager.DeleteExtentrixWindowsCredentials(Page, LogLocation, SPContext.Current.Web.CurrentUser);
                                LoadCredentialsView(true);
                            }
                        }
                        else
                        {
                            var isValid = true;
                            if (!Page.IsPostBack)
                            {
                                var service = new WebServiceAPI();
                                if (service.IsCredentialsValid((WindowsCredentials)credentials))
                                {
                                    Credentials = credentials;
                                }
                                else
                                {
                                    isValid = false;
                                    SecureStoreManager.DeleteExtentrixWindowsCredentials(Page, LogLocation, SPContext.Current.Web.CurrentUser);
                                }
                            }
                            if (isValid)
                            {
                                wp.launchApplication(Credentials, id, hostName, frmName);
                            }
                            else
                            {
                                LoadCredentialsView(false);
                            }
                        }
                    }
                    else
                    {
                        LoadCredentialsView(SecureStoreManager.IsCredentialsExpired(CredentialLiveTime));
                    }
                }

                return;
            }
            else
            {
                try
                {
                    if (!IsUseSecureStore)
                    {
                        if (Credentials == null)
                        {
                            Credentials = wp.GetCredentials(SPContext.Current.Web.CurrentUser);
                        }
                        LoadWebPartView(wp);
                    }
                    else
                    {
                        var credentials = Credentials ?? SecureStoreManager.GetExtentrixWindowsCredentials(Page, LogLocation, SPContext.Current.Web.CurrentUser);
                        if (credentials != null)
                        {
                            if (CredentialLiveTime != -1)
                            {
                                if (!SecureStoreManager.IsCredentialsExpired(CredentialLiveTime))
                                {
                                    Credentials = credentials;
                                    LoadWebPartView(wp);
                                }
                                else
                                {
                                    SecureStoreManager.DeleteExtentrixWindowsCredentials(Page, LogLocation, SPContext.Current.Web.CurrentUser);
                                    LoadCredentialsView(true);
                                }
                            }
                            else
                            {
                                var isValid = true;
                                if (!Page.IsPostBack)
                                {
                                    var service = new WebServiceAPI();
                                    if (service.IsCredentialsValid((WindowsCredentials)credentials))
                                    {
                                        Credentials = credentials;
                                    }
                                    else
                                    {
                                        isValid = false;
                                        SecureStoreManager.DeleteExtentrixWindowsCredentials(Page, LogLocation, SPContext.Current.Web.CurrentUser);
                                    }
                                }
                                if (isValid)
                                {
                                    LoadWebPartView(wp);
                                }
                                else
                                {
                                    LoadCredentialsView(false);
                                }
                            }
                        }
                        else
                        {
                            LoadCredentialsView(SecureStoreManager.IsCredentialsExpired(CredentialLiveTime));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Default.Error(LogLocation, "User - " + Page.Request.LogonUserIdentity.Name + " ; Error = " + ex.Message, ex);
                    AppMultiView.ActiveViewIndex = 5;
                }
            }
        }