public static OAuth2CredentialCollection Load(ApplicationConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            OAuth2CredentialCollection list = null;

            lock (configuration.PropertiesLock)
            {
                object value = null;

                if (configuration.Properties.TryGetValue("OAuth2Credentials", out value))
                {
                    list = value as OAuth2CredentialCollection;
                }

                if (list == null)
                {
                    list = configuration.ParseExtension <OAuth2CredentialCollection>();

                    if (list == null)
                    {
                        list = new OAuth2CredentialCollection();
                    }

                    configuration.Properties["OAuth2Credentials"] = list;
                }
            }

            return(list);
        }
        public static OAuth2Credential FindByAuthorityUrl(ApplicationConfiguration configuration, string authorityUrl)
        {
            if (authorityUrl == null || !Uri.IsWellFormedUriString(authorityUrl, UriKind.Absolute))
            {
                throw new ArgumentException("authorityUrl");
            }

            if (!authorityUrl.EndsWith("/"))
            {
                authorityUrl += "/";
            }

            OAuth2CredentialCollection list = Load(configuration);

            if (list != null)
            {
                foreach (var ii in list)
                {
                    // this is too allow generic sample config files to work on any machine.
                    // in a real system explicit host names would be used so this would have no effect.
                    var uri = ii.AuthorityUrl.Replace("localhost", System.Net.Dns.GetHostName().ToLowerInvariant());

                    if (!uri.EndsWith("/"))
                    {
                        uri += "/";
                    }

                    if (String.Compare(uri, authorityUrl, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        var credential = new OAuth2Credential()
                        {
                            AuthorityUrl          = authorityUrl,
                            GrantType             = ii.GrantType,
                            ClientId              = ii.ClientId,
                            ClientSecret          = ii.ClientSecret,
                            RedirectUrl           = ii.RedirectUrl,
                            TokenEndpoint         = ii.TokenEndpoint,
                            AuthorizationEndpoint = ii.AuthorizationEndpoint
                        };

                        return(credential);
                    }
                }
            }

            return(null);
        }
        public static OAuth2Credential FindByServerUri(ApplicationConfiguration configuration, string serverApplicationUri)
        {
            if (serverApplicationUri == null || !Uri.IsWellFormedUriString(serverApplicationUri, UriKind.Absolute))
            {
                throw new ArgumentException("serverApplicationUri");
            }

            OAuth2CredentialCollection list = Load(configuration);

            if (list != null)
            {
                foreach (var ii in list)
                {
                    if (ii.Servers != null && ii.Servers.Count > 0)
                    {
                        foreach (var jj in ii.Servers)
                        {
                            // this is too allow generic sample config files to work on any machine.
                            // in a real system explicit host names would be used so this would have no effect.
                            var uri = jj.ApplicationUri.Replace("localhost", System.Net.Dns.GetHostName().ToLowerInvariant());

                            if (uri == serverApplicationUri)
                            {
                                var credential = new OAuth2Credential()
                                {
                                    AuthorityUrl          = ii.AuthorityUrl,
                                    GrantType             = ii.GrantType,
                                    ClientId              = ii.ClientId,
                                    ClientSecret          = ii.ClientSecret,
                                    RedirectUrl           = ii.RedirectUrl,
                                    TokenEndpoint         = ii.TokenEndpoint,
                                    AuthorizationEndpoint = ii.AuthorizationEndpoint,
                                    SelectedServer        = jj
                                };

                                return(credential);
                            }
                        }
                    }
                }
            }

            return(null);
        }