/// <summary>
        /// Imports the specified file containing an XML configuration of the SSO application.
        /// </summary>
        /// <param name="filePath">Import file path.</param>
        /// <param name="overrideApp">Value indicating whether to override the application if it already exists.</param>
        /// <returns><b>true</b> if the application imported successfully; otherwise <b>false</b>.</returns>
        public static bool ImportApplication(string filePath, bool overrideApp)
        {
            // load the app configuration from the file
            SSOAppConfig appConfig = XmlSerializationUtil.LoadXml <SSOAppConfig>(filePath);

            // check if the application already exists
            if (SSOManager.ApplicationExists(appConfig.AppInfo.Name) == true)
            {
                if (overrideApp == true)
                {
                    // update/recreate the application
                    SSOManager.UpdateApplication(appConfig, true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                // create a new application
                SSOManager.CreateApplication(appConfig);
            }
            return(true);
        }
        /// <summary>
        /// Updates the specified application including both, metadata information
        /// and fields.
        /// </summary>
        /// <param name="appConfig">Configuration information used to update the application.</param>
        /// <param name="recreate">Value indicating wheter to recreate the application.</param>
        public static void UpdateApplication(SSOAppConfig appConfig, bool recreate)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                // create SSO objects
                ISSOAdmin2      ssoAdmin       = new ISSOAdmin2();
                ISSOConfigStore ssoConfigStore = new ISSOConfigStore();

                // enlist them in the transaction
                SSOManager.Enlist(ssoAdmin as IPropertyBag, Transaction.Current);
                SSOManager.Enlist(ssoConfigStore as IPropertyBag, Transaction.Current);

                // check if the application needs to be recreated or just updated
                if (recreate == true)
                {
                    // delete and recreate
                    SSOManager.DeleteApplication(ssoAdmin, appConfig.AppInfo.Name);
                    SSOManager.CreateApplication(ssoAdmin, appConfig);
                }
                else
                {
                    // just update the application metadata
                    SSOManager.UpdateApplicationInfo(ssoAdmin, appConfig.AppInfo);
                }

                // update the application fields
                ssoConfigStore.SetConfigInfo(appConfig.AppInfo.Name, SSOManager.ConfigIdentifier, appConfig.AppFields);
                // commit the transaction
                transactionScope.Complete();
            }
        }
        /// <summary>
        /// Exports the specified SSO application into the specified file in XML format.
        /// </summary>
        /// <param name="appName">The name of the application to be deleted.</param>
        /// <param name="filePath">Export file path.</param>
        public static void ExportApplication(string appName, string filePath)
        {
            // load the specified SSO application configuration
            SSOAppConfig appConfig = SSOManager.GetApplicationConfig(appName);

            // save it into the specified file
            XmlSerializationUtil.SaveXml(appConfig, filePath);
        }
        public static void UpdateApplicationFields(string appName, SSOAppFieldCollection appFields, bool recreate)
        {
            if (recreate == true)
            {
                SSOAppConfig appConfig = new SSOAppConfig()
                {
                    // load the current application metadata information
                    AppInfo = SSOManager.GetApplicationInfo(appName),
                    // and use the application fields provided
                    AppFields = appFields
                };

                // update/recreate the application including the fields
                SSOManager.UpdateApplication(appConfig, true);
            }
            else
            {
                // just update the application fields
                ISSOConfigStore ssoConfigStore = new ISSOConfigStore();
                ssoConfigStore.SetConfigInfo(appName, SSOManager.ConfigIdentifier, appFields);
            }
        }
        /// <summary>
        /// Creates a new application in the SSO store using specified
        /// ISSOAdmin2 object instance and the configuration information.
        /// Creates both, the application and fields.
        /// </summary>
        /// <remarks>
        /// For internal use only. Assumes a TransactionScope is created before
        /// calling this method.
        /// </remarks>
        /// <param name="ssoAdmin">ISSOAdmin2 instance.</param>
        /// <param name="appConfig">Configuration information used to create the application.</param>
        private static void CreateApplication(ISSOAdmin2 ssoAdmin, SSOAppConfig appConfig)
        {
            // set default contact
            appConfig.AppInfo.Contact = SSOManager.DefaultContact;

            // fix the fieldCount when creating an application with no fields
            int fieldCount = Math.Max(1, appConfig.AppFields.Count);

            // create the sso application
            ssoAdmin.CreateApplication(
                appConfig.AppInfo.Name,
                appConfig.AppInfo.Description,
                appConfig.AppInfo.Contact,
                appConfig.AppInfo.UserAccounts,
                appConfig.AppInfo.AdminAccounts,
                appConfig.AppInfo.Flags & ~SSOFlag.SSO_FLAG_ENABLED, // the SSO_FLAG_ENABLED flag cannot be specified when creating an SSO application
                fieldCount);

            // create dummy field in the first slot
            ssoAdmin.CreateFieldInfo(appConfig.AppInfo.Name, "(unused)", SSOFlag.SSO_FLAG_NONE);
            // create the actual fields
            foreach (SSOAppField field in appConfig.AppFields)
            {
                // set field flags
                int fieldFlags = SSOFlag.SSO_FLAG_NONE;
                if (field.Masked == true)
                {
                    fieldFlags |= SSOFlag.SSO_FLAG_FIELD_INFO_MASK;
                }
                // create it
                ssoAdmin.CreateFieldInfo(appConfig.AppInfo.Name, field.Name, fieldFlags);
            }

            // enable application
            if (appConfig.AppInfo.Enabled == true)
            {
                ssoAdmin.UpdateApplication(appConfig.AppInfo.Name, null, null, null, null, SSOFlag.SSO_FLAG_ENABLED, SSOFlag.SSO_FLAG_ENABLED);
            }
        }
        /// <summary>
        /// Creates a new application in the SSO store using specified
        /// the configuration information. Creates both, the application
        /// and fields.
        /// </summary>
        /// <param name="appConfig">Configuration information used to create the application.</param>
        public static void CreateApplication(SSOAppConfig appConfig)
        {
            // create a transaction
            using (TransactionScope transactionScope = new TransactionScope())
            {
                // create SSO objects
                ISSOAdmin2 ssoAdmin = new ISSOAdmin2();

                // enlist them in the transaction
                SSOManager.Enlist(ssoAdmin as IPropertyBag, Transaction.Current);

                // create the sso application
                SSOManager.CreateApplication(ssoAdmin, appConfig);

                // commit the transaction
                transactionScope.Complete();
            }

            // update the application fields
            ISSOConfigStore ssoConfigStore = new ISSOConfigStore();

            //SSO.Enlist(ssoConfigStore as IPropertyBag, Transaction.Current);
            ssoConfigStore.SetConfigInfo(appConfig.AppInfo.Name, SSOManager.ConfigIdentifier, appConfig.AppFields);
        }