private static IDictionary <string, IdentityProviderClientConfigurationElement> GetIdentityProvidersFromConfig()
        {
            IDictionary <string, IdentityProviderClientConfigurationElement> identityProvierLookUp = new Dictionary <string, IdentityProviderClientConfigurationElement>();

            RetailConfiguration retailConfiguration = (RetailConfiguration)OpenIdConnectUtilities.DynamicsConnectorConfiguration.GetSection(OpenIdConnectUtilities.ConfigurationSectionName);

            foreach (IdentityProviderClientConfigurationElement provider in retailConfiguration.IdentityProviders)
            {
                identityProvierLookUp.Add(provider.Name, provider);
            }

            return(identityProvierLookUp);
        }
        /// <summary>
        /// Gets the identity provider from configuration.
        /// </summary>
        /// <param name="name">The name of identity provider.</param>
        /// <returns>The identity provider client configuration element.</returns>
        public static IdentityProviderClientConfigurationElement GetIdentityProviderFromConfiguration(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }

            RetailConfiguration retailConfiguration = (RetailConfiguration)DynamicsConnectorConfiguration.GetSection(OpenIdConnectUtilities.ConfigurationSectionName);

            foreach (IdentityProviderClientConfigurationElement provider in retailConfiguration.IdentityProviders)
            {
                if (provider.Name == name)
                {
                    return(provider);
                }
            }

            throw new InvalidOperationException("Current Identity Provider was not found, this could happen due to timeout, login again.");
        }
        /// <summary>
        /// Configures the authentication.
        /// </summary>
        /// <param name="app">The application.</param>
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = OpenIdConnectUtilities.ApplicationCookieAuthenticationType,
                LoginPath          = new PathString("/Login"),
                CookieSecure       = CookieSecureOption.Always
            });

            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            // Loading Retail Configuration to access available Identity Providers.
            RetailConfiguration retailConfiguration = (RetailConfiguration)OpenIdConnectUtilities.DynamicsConnectorConfiguration.GetSection(OpenIdConnectUtilities.ConfigurationSectionName);

            if (retailConfiguration == null)
            {
                return;
            }

            try
            {
                // Enumerating all available Identity Providers and creating a middleware for each of the provider.
                foreach (IdentityProviderClientConfigurationElement provider in retailConfiguration.IdentityProviders)
                {
                    if (provider.ProviderType == IdentityProviderType.OpenIdConnect)
                    {
                        OpenIdConnectAuthenticationOptions options = GetOpenIdConnectAuthenticationOptions(provider);
                        app.UseOpenIdConnectAuthentication(options);
                    }
                }
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error(ex.Message, ex, this);
            }
        }